r shiny - uiOutput没有在menuItem

时间:2016-04-14 02:51:52

标签: r shiny shinydashboard

问题

为什么在sliderInput()上生成server并在ui uiOutput()上呈现的menuItem()未显示在sliderInput中?

示例

在这个简单的应用中,我在server上生成menuItem(注意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) 是故意注释掉的),并且按预期工作

uiOutput

但是,当我在menuItem( )内移动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() ) 时,输出不再呈现:

[uid@localhost bokeh_apps]$ bokeh serve --show myapp --port 5100 --host 127.0.0.1:80

1 个答案:

答案 0 :(得分:4)

首先,我将uiOutput("sli_val1")更改为uiOutput("out_sli_val1"),以防止重复的ID。

关于你的问题:当Shiny在文档中运行并呈现/绑定所有可能的输出时,这是一个奇怪的事情。默认操作是忽略所有隐藏的输出元素 - 这意味着不要完全忽略(输出“sli_val1”绑定正常),但是它们的功能被暂停,不允许渲染子项。

所以问题是,在启动时,此输出隐藏在子项目树中,并从那里被忽略。

可以通过调用

取消设置此超级行为来完成修复
outputOptions(output, "out_sli_val1", suspendWhenHidden = FALSE)

但是如何以及在哪里?必须在运行“数据加载”之前设置此选项。但如果output$out_sli_val1没有分配任何内容(NULL),则会抛出错误。为了避免这种情况,可以初始化一个空的UI块,它会在“数据加载”上被覆盖。

以下是您的最小修补程序,其中包含2行额外代码(以及修改后的输出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)