r - shinyjs:当条件不评估为 TRUE 时,切换不会隐藏自定义 UI
问题描述
我正在使用shinyjs
pkg 和各种隐藏/显示自定义 UI 的选项。在下面的应用程序中,我想显示选定的图,如果未选择任何选项,则显示一个空白区域。我使用toggle()
,只要选择了至少一个选项,它就可以解决问题checkboxGroupInput
,但是当没有选择任何选项时,两个图都会显示。根据文件:
如果给定条件来切换,则该条件将用于确定是显示还是隐藏元素。如果条件评估为 TRUE,则将显示该元素,否则将隐藏该元素。
我在这里遗漏了一些明显的东西吗?
library(shiny)
library(shinyjs)
library(ggplot2)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
checkboxGroupInput(inputId = 'options',
label = 'Choose your plot(s)',
choices = list("Plot 1" = 1,
"Plot2" = 2)),
#selected = 1:2),
verbatimTextOutput('checkbox_text'),
uiOutput("Ui1"),
uiOutput('Ui2')
)
)
server <- function(input, output, session) {
output$checkbox_text <- renderText({
paste(input$options)
})
observe({
shinyjs::toggle(id = "Ui1", condition = input$options == 1)
shinyjs::toggle(id = "Ui2", condition = input$options == 2)
})
output$Ui1 <- renderUI({
output$plot1 <- renderPlot({
p <- ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
geom_smooth() +
ggtitle('Plot 1')
p
})
plotOutput('plot1')
})
output$Ui2 <- renderUI({
output$plot2 <- renderPlot({
p<- ggplot(mtcars, aes(disp, mpg, colour = as.factor(cyl))) +
geom_point() +
ggtitle('Plot 2')
p
})
plotOutput('plot2')
})
}
shinyApp(ui, server)
解决方案
当您正在寻找选中哪些框时checkboxGroupInput
,通常最好评估您感兴趣的值是否等于其中的任何值checkboxGroupInput
- 用此替换您的observe
语句将解决问题:
observe({
shinyjs::toggle(id = "Ui1", condition = {1 %in% input$options})
shinyjs::toggle(id = "Ui2", condition = {2 %in% input$options})
})
就是说,我不确定为什么TRUE
在您的情况下没有选中任何一个框都会评估为。
推荐阅读
- java - Mockito 的 @After 和 verifyNoMoreInteractions
- apache-spark - 使用 Azure Databricks 写入(作为 .csv)到 Blob 存储时修改数据框名称
- mysql - 在不使用 LIMIT x,x 的情况下获取接下来的 10 条记录
- c# - WebClient.DownloadString() 时出现 500 错误
- ruby-on-rails - Docker Rails 应用程序上没有这样的文件或目录@rb_sysopen sidekiq.pid
- python - /bin/sh: python: 找不到命令
- c++ - Ctrl + M + O 折叠所有功能的快捷方式在 Visual Studio 2017 中不起作用
- parsing - 算术表达式和赋值的 LL1 语法
- java - 在 java 中向数组提供元素时,最佳实践是什么?
- javascript - JS - 将数组中的唯一列与重复项连接起来