1

My application generates both automatic Ui and can also add ui manually. I have used both renderUI and InsertUI in the main application to call shiny modules.

The normal use case of shiny module even with renderUI function in module server updates my values nicely. But when I am generating shiny modules by invoking insertUI and renderUI function, the input values(selectInput, numericInput) are not updating. I am not understanding the reason for this, can anyone answer my problem.

Here is my workable code

Problem observing Shiny Module update inputs

# Module UI

  ui_module <- function(id){
  ns <- NS(id)

  fluidRow(
         uiOutput(ns("Input_ui"))
          )
   }

# Server UI

 server_module <- function(input,output,session){
  ns <- session$ns

   output$Input_ui <- renderUI({
     list(
       tags$div(id = ns("input_div"),numericInput(ns("Input"),"Number",NA,value = 537153, step = 1)),
       tags$div(id = ns("input2_div"),numericInput(ns("Input2"),"Number2",NA,value=686575,step = 1))
     )
  })

## INPUTS are not updating ##

 observe({
      updateNumericInput(session,
                     "Input", "Number",value = 4, step = 1)
      updateNumericInput(session,
                      "Input2", "Number2",value = 8.9, step = 1)
   })
 }

 # App UI

 ui <- fluidPage(
    fluidRow(id = "Row",
      uiOutput("ui"),
      actionButton("add","ADD")
    )
  )

# Server UI

 server <- function(input,output,session){
 # Initiating counter 
   n <- 0
 # One by one adding the modules

       observeEvent(input$add,{
             n <<- n + 1
             panels <- paste0("panels_new",n)
             insertUI("#Row",
                      "beforeEnd",
                       ui_module(panels))

             callModule(server_module,panels)
         })

  # Generating a no of shiny modules based on the some table rows

   output$ui <- renderUI({
           n <- 2                                # n will be nrow in my main app.
           list <- as.list(1:n)
           lapply(list, function(i){
                panels <- paste0("panels",i)
             fluidRow(
                 ui_module(panels)
         )
       })
     })

   observe({
           n <- 2                                # n will be nrow in my main app.
           list <- as.list(1:n)
           lapply(list, function(i){
           panels <- paste0("panels",i)
           callModule(server_module,panels)
    })
  })
 }

shinyApp(ui,server)

Normal Use case of Shiny Module (updation works)

 #Module UI

 ui_module <- function(id){
   ns <- NS(id)

   fluidRow(
          uiOutput(ns("Input_ui"))
         )
    }
#Module Server

server_module <- function(input,output,session){
 ns <- session$ns

 output$Input_ui <- renderUI({
      list(
    tags$div(id = ns("input_div"),numericInput(ns("Input"), "Number",NA,value = 537153, step = 1)),
    tags$div(id = ns("input2_div"),numericInput(ns("Input2"),"Number2",NA,value = 686575, step =1))
  )
})

 observe({
     updateNumericInput(session,
                   "Input", "Number",value = 4, step = 1)    
     updateNumericInput(session,
                   "Input2", "Number2",value = 8.9, step = 1)
     })  
}

#APP UI

ui <- fluidPage(
    fluidRow(id = "Row",
          ui_module("panels")  
     )
  )

#APP Server

server <- function(input,output,session){
    callModule(server_module,"panels")
 }
 shinyApp(ui,server) 
Jitu Deka
  • 65
  • 8

1 Answers1

0
  • Your module server function isn't returning anything, so the main server module doesn't know that anything has changed.
  • You don't need to namespace the inputs in the module server function. They're already namespaced
  • What do you want to update and when? it's not at all clear...
Limey
  • 10,234
  • 2
  • 12
  • 32
  • First of all I am really sorry for forgetting to include the following CSS line just after fluidRow in the APP UI. `inlineCSS(".row {margin:60px 0;} div[id *= 'input_div']{position:absolute;left:6em;width:200px;height:30px;} div[id *= 'input2_div']{position:absolute;left:20em;width:200px;height:30px; div[id *= 'Def_id']{position:absolute;left:30em;width:200px;height:30px;"),` The input panels will not appear properly without theses lines. – Jitu Deka May 24 '20 at 13:06
  • **First Point :** My module server is working because if I pass a table from the app server than it is displaying the table as datatdableoutput from the module server. @Limey – Jitu Deka May 24 '20 at 13:27
  • **Second Point:** I did not understand your namespace reference in the module server. I have to use the input namespace in the module server because of the uioutput defined in the module ui. @Limey – Jitu Deka May 24 '20 at 13:32
  • **Third Point:** I want the values given in the observe section in the module server to be displayed and not the original values in the renderUI section in the module server as soon as we open the APP. – Jitu Deka May 24 '20 at 13:37