用户选择/多输入上的多个单独图
Posted
技术标签:
【中文标题】用户选择/多输入上的多个单独图【英文标题】:Multiple Separate Plots on User select/multi input 【发布时间】:2021-07-16 00:07:57 【问题描述】:我想根据用户输入来可视化绘图。我有一个下拉菜单,如果用户从给定的选项中选择一个或多个变量,代码会根据用户输入自动可视化每个变量的单独图。
代码:这就是我尝试的方式。如果有其他方法,请提出建议。
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时,它们没有隐藏。
请帮忙。谢谢。
【问题讨论】:
您能否尝试使用mtcars
之类的数据集创建minimal reproducible example,以便您的代码运行?
@Waldi ,您好,我已按照您的要求更新了代码并为您提问,以便您也可以尝试一下。
查看我的编辑以在选择前隐藏绘图
@Waldi ,您编辑的代码完成了所需的工作。非常感谢您的帮助,非常感谢。
或许也可以看看***.com/a/24234636/13513328
【参考方案1】:
由于选择允许多选,您应该使用%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))
)
)
【讨论】:
以上是关于用户选择/多输入上的多个单独图的主要内容,如果未能解决你的问题,请参考以下文章
可以使用在同一页面上的表单中选择的值填充 php/mysql 调用吗?
多用户MIMO系统中的天线选择技术和用户选择技术的MATLAB仿真