r shiny - uiOutput not rendering inside menuItem

北城以北 提交于 2019-12-31 02:55:26

问题


Question

Why does a sliderInput() that's generated on the server, and rendered on the ui with uiOutput() not get displayed in a menuItem()?

Example

In this simple app I'm generating a sliderInput on the server (note the menuItem is deliberately commented out), and it works as expected

library(shiny)
library(shinydashboard)

rm(ui, server)

ui <- dashboardPage(
    dashboardHeader(),

    dashboardSidebar(
        sidebarMenu(
            #menuItem(text = "data options",
                      checkboxGroupInput(inputId = "cbg_group1", label = "group 1", 
                                         choices = c("some","check","boxes","to","choose","from") ),
                       uiOutput("sli_val1"),
                       checkboxGroupInput(inputId = "cbg_group2", label = "group 2", 
                                          choices = c("another","set","of","check","boxes") ),
            #                ),
            menuItem(text = "another tab")
            )
    ),
    dashboardBody()
)

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

    withProgress(message = "loading page", value=0.1, {
        ## simulate loading some data
        Sys.sleep(3)

        ## slider input
        output$sli_val1 <- renderUI({
            sliderInput(inputId = "sli_val1", 
                        label = "values", min = 0, max = 100,
                        value = c(25, 75) )
        })
        setProgress(value=1, detail="Complete")
    })
}

shinyApp(ui = ui, server = server)

However, when I move the uiOutput inside a menuItem( ), the output no longer renders:

ui <- dashboardPage(
    dashboardHeader(),

    dashboardSidebar(
        sidebarMenu(
            menuItem(text = "data options",
                     checkboxGroupInput(inputId = "cbg_group1", label = "group 1", 
                                        choices = c("some","check","boxes","to","choose","from") ),
                     uiOutput("sli_val1"),
                     checkboxGroupInput(inputId = "cbg_group2", label = "group 2", 
                                        choices = c("another","set","of","check","boxes") )
                             ),
            menuItem(text = "another tab")
            )
    ),
    dashboardBody()
)

回答1:


At first, I'd change uiOutput("sli_val1") into uiOutput("out_sli_val1"), to prevent duplicated Ids.

Concerning your problem: This is an odd thing going on when Shiny runs through the document and renders/binds all possible outputs. The default action is to ignore all hidden output elements - that means not ignore completely (output "sli_val1" is bound alright), but their function is suspended, letting no children to be rendered.

So the problem is, that on initiation, this output is hidden in the subitem tree and from there on ignored.

The fix can be done by unsetting this supension behaviour by calling

outputOptions(output, "out_sli_val1", suspendWhenHidden = FALSE)

But how and where? This option has to be set before you run your "data loading". But it will throw errors, if output$out_sli_val1 has nothing assigned to it (is NULL). To avoid that, one can initialize an empty UI-chunk, which gets overridden on "data load".

Here is your minimal fix with 2 extra lines of code (and modified output Id):

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem(text = "data options", 
        checkboxGroupInput(inputId = "cbg_group1", label = "group 1", 
                           choices = c("some","check","boxes","to","choose","from") ),
        uiOutput("out_sli_val1"),
        checkboxGroupInput(inputId = "cbg_group2", label = "group 2", 
                           choices = c("another","set","of","check","boxes") )
      ),
      menuItem(text = "another tab")
    )
  ),
  dashboardBody()
)

server <- function(input, output, session){
  output$out_sli_val1 <- renderUI({})
  outputOptions(output, "out_sli_val1", suspendWhenHidden = FALSE)

  withProgress(message = "loading page", value=0.1, {
    ## simulate loading some data
    Sys.sleep(3)

    ## slider input
    output$out_sli_val1 <- renderUI({
      sliderInput(inputId = "sli_val1", label = "values", min = 0, max = 100, value = c(25, 75) )
    })

    setProgress(value=1, detail="Complete")
  })
}

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/36613018/r-shiny-uioutput-not-rendering-inside-menuitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!