如何解决用户选择/多输入的多个独立图
我想根据用户输入可视化绘图。我有一个下拉菜单,如果用户从给定的选项中选择一个或多个变量,代码会根据用户输入自动可视化每个变量的单独图。
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),#Necessary to activate shinyjs
selectInput("select","Select plot:",1:4,multiple = TRUE),plotOutput("p1"),plotOutput("p2"),plotOutput("p3"),plotOutput("p4")
),server = function(input,output) {
output$p1 <- renderPlot({ plot(iris) })
output$p2 <- renderPlot({ plot(mtcars) })
output$p3 <- renderPlot({ plot(0) })
output$p4 <- renderPlot({ plot(1) })
observeEvent(input$select,{
req(input$select)
shinyjs::toggle("p1",condition = input$select == 1)
shinyjs::toggle("p2",condition = input$select == 2)
shinyjs::toggle("p3",condition = input$select == 3)
shinyjs::toggle("p4",condition = input$select == 4)
})
}
)
这段代码的问题是,当我从下拉菜单中选择任何一个输入时,其他变量的所有其他图也会显示出来。 加上所有变量选择并且我尝试从下拉菜单中取消选择变量/ s时,它们没有隐藏。
请帮忙。谢谢。
解决方法
由于选择允许多项选择,您应该使用 %in%
而不是 ==
。
您还应该使用 observe
而不是 observeEvent
对 NULL
输入做出反应。
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),#Necessary to activate shinyjs
selectizeInput("select","Select plot:",1:4,multiple = TRUE),plotOutput("p1"),plotOutput("p2"),plotOutput("p3"),plotOutput("p4")
),server = function(input,output) {
output$p1 <- renderPlot({ plot(iris) })
output$p2 <- renderPlot({ plot(mtcars) })
output$p3 <- renderPlot({ plot(0) })
output$p4 <- renderPlot({ plot(1) })
observe({
shinyjs::toggle("p1",condition = isTRUE(1 %in% input$select))
shinyjs::toggle("p2",condition = isTRUE(2 %in% input$select))
shinyjs::toggle("p3",condition = isTRUE(3 %in% input$select))
shinyjs::toggle("p4",condition = isTRUE(4 %in% input$select))
})
}
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。