Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Modules not working with renderUI

Tags:

r

shiny

I am using renderUI to optionally present a Table or Plot based on user selection of the visualization option. I am also using Shiny modules to present the same thing on multiple tabs. While I have gotten Shiny modules to work wonderfully in another app, I am struggling to get it to work with renderUI.

Here is a minimal piece of code that I came up with that shows the problem where nothing gets displayed on either tabs:

myUI <- function(id) {
  ns <- NS(id)
  fluidRow(
    uiOutput(ns('myFinalText'))
  )
}

ui <- fluidPage(
  tabBox(id = 'myBox', width = 12,
         tabPanel('Tab1',
                  fluidRow(
                    myUI('tab1')
                  )),
         tabPanel('Tab2',
                  fluidRow(
                    myUI('tab2')
                  ))
         )
)

myTextFunc <- function(input, output, session, text) {
  output$myFinalText <- renderUI({
    output$myText <- renderText({text})
    textOutput('myText')
  })
}

server <- function(input, output, session) {
  callModule(myTextFunc, 'tab1', session = session, 'Hello Tab1')
  callModule(myTextFunc, 'tab2', session = session, 'Hello Tab2')
}

shinyApp(ui = ui, server = server)

Any thoughts on what else I should be doing to make this work?

Replacing the Shiny module UI function and server functions as follows makes it work fine.

myUI <- function(id) {
  ns <- NS(id)
  fluidRow(
    textOutput(ns('myFinalText'))
  )
}

myTextFunc <- function(input, output, session, text) {
  output$myFinalText <- renderText({
    text
  })
}
like image 296
Gopala Avatar asked Sep 01 '16 13:09

Gopala


1 Answers

You can get the namespace from the session object. Change myTextFunc in the initial app like this:

myTextFunc <- function(input, output, session, text) {
    ns <- session$ns
    output$myFinalText <- renderUI({
        output$myText <- renderText({text})
        textOutput(ns('myText'))
    })
}
like image 145
nokiddn Avatar answered Sep 18 '22 02:09

nokiddn