mathejoachim
mathejoachim

Reputation: 51

Using shiny's renderUI in Module

this is my first question at stackoverflow. I have a problem with modules and renderUI in shiny (1.0.5).

When I use renderUI in

#### Main Part

ui <- bootstrapPage(
  uiOutput("DynamicContent")
)

server <- function(input, output,session) {

  S_A <- selectInput("S_A_Input"  ,"Change Me for print message",choices=1:3 )

  output$DynamicContent <- renderUI({
    tagList(S_A) 
  })

  observe({
    print(input$S_A_Input) 
  })

}

shinyApp(ui = ui, server = server)

then changing the selectInput will cause changing input$S_A_Input, so the print will occur. That's fine.

On the other hand, input$S_A_Input seems not to work, if I work with modules:

### Module Part

Module_YYY_Server <- function(input, output, session){ 

  S_A <- selectInput("S_A_Input"  ,"Change Me for print message",choices=1:3 )

  output$DynamicContent <- renderUI({
    tagList(S_A) 
  })

  observe({
    print(input$S_A_Input) 
  })


}


Module_YYY_Ui <- function(id){

  ns <- NS(id) # Creates Namespace

  tagList(
    uiOutput("DynamicContent" %>% ns)
  )

}

And then calling the module.

#### Main Part

ui <- bootstrapPage(
  Module_YYY_Ui("YYY")  
)

server <- function(input, output,session) {
  callModule(Module_YYY_Server,"YYY")
}

shinyApp(ui = ui, server = server)

I haven't found a solution to this behaviour.

Upvotes: 4

Views: 3344

Answers (2)

yuskam
yuskam

Reputation: 410

Late to the party but this is another option to solve your problem using

session$ns("id")

Here how it looks like:

Module_YYY_Server <- function(input, output, session){

  output$DynamicContent <- renderUI({
    selectInput(session$ns("S_A_Input"), "Change Me for print message", choices = 1:3)
    })

  output$text <- renderText({
    req(input$S_A_Input)
    input$S_A_Input})
}


Module_YYY_Ui <- function(id){

  ns <- NS(id) # Creates Namespace

  tagList(
    uiOutput(ns("DynamicContent")),
    textOutput(ns("text"))
  )
}


ui <- bootstrapPage(
  Module_YYY_Ui("YYY")
)

server <- function(input, output,session) {
  callModule(Module_YYY_Server,"YYY")
}

shinyApp(ui = ui, server = server)

This is based on the example here

Upvotes: 9

tbradley
tbradley

Reputation: 2290

While I am sure your example is simplified, If your input$S_A_Input is not changing based on what you are doing, as is the case in your example, I would not recommend using renderUI. Regardless of that, the reason that your input is not printing is because you are not generating it as an output.

Module_YYY_Server <- function(input, output, session){

  output$DynamicContent <- renderUI({
    ns <- session$ns
    tagList(
      selectInput("S_A_Input" %>% ns, "Change Me for print message",choices=1:3 )
    ) 
  })

  output$text <- renderText({input$S_A_Input})



}


Module_YYY_Ui <- function(id){

  ns <- NS(id) # Creates Namespace

  tagList(
    uiOutput("DynamicContent" %>% ns),
    textOutput("text" %>% ns)
  )

}

Upvotes: 3

Related Questions