r shiny - uiOutput 不在 menuItem 中呈现
r shiny - uiOutput not rendering inside menuItem
问题
为什么在 server
上生成并在 ui
上使用 uiOutput()
呈现的 sliderInput()
不会显示在 menuItem()
中?
例子
在这个简单的应用程序中,我在 server
上生成了一个 sliderInput
(注意 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()
)
首先,我会将 uiOutput("sli_val1")
更改为 uiOutput("out_sli_val1")
,以防止 ID 重复。
关于您的问题:当 Shiny 运行 遍历文档和 renders/binds 所有可能的输出时,这是一件奇怪的事情。默认操作是忽略所有隐藏的输出元素——这意味着不完全忽略(输出 "sli_val1" 绑定好),但它们的功能被暂停,不让任何 children 被渲染。
所以问题是,在启动时,此输出隐藏在子项树中,并从那里被忽略。
可以通过调用
取消设置此暂停行为来完成修复
outputOptions(output, "out_sli_val1", suspendWhenHidden = FALSE)
但是如何以及在哪里?此选项必须在 运行 和 "data loading" 之前设置。但如果 output$out_sli_val1
没有分配任何内容(是 NULL
),它会抛出错误。为避免这种情况,可以初始化一个空 UI-chunk,它在 "data load" 上被覆盖。
这是你的最小修复,增加了 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)
问题
为什么在 server
上生成并在 ui
上使用 uiOutput()
呈现的 sliderInput()
不会显示在 menuItem()
中?
例子
在这个简单的应用程序中,我在 server
上生成了一个 sliderInput
(注意 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()
)
首先,我会将 uiOutput("sli_val1")
更改为 uiOutput("out_sli_val1")
,以防止 ID 重复。
关于您的问题:当 Shiny 运行 遍历文档和 renders/binds 所有可能的输出时,这是一件奇怪的事情。默认操作是忽略所有隐藏的输出元素——这意味着不完全忽略(输出 "sli_val1" 绑定好),但它们的功能被暂停,不让任何 children 被渲染。
所以问题是,在启动时,此输出隐藏在子项树中,并从那里被忽略。
可以通过调用
取消设置此暂停行为来完成修复outputOptions(output, "out_sli_val1", suspendWhenHidden = FALSE)
但是如何以及在哪里?此选项必须在 运行 和 "data loading" 之前设置。但如果 output$out_sli_val1
没有分配任何内容(是 NULL
),它会抛出错误。为避免这种情况,可以初始化一个空 UI-chunk,它在 "data load" 上被覆盖。
这是你的最小修复,增加了 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)