【问题标题】: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 ,您编辑的代码完成了所需的工作。非常感谢您的帮助,非常感谢。

标签: r shiny shinyjs selectinput rshiny


【解决方案1】:

由于选择允许多选,您应该使用%in% 而不是==
您还应该使用observe 而不是observeEventNULL 输入做出反应。

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))
    })
    
  }
)

【讨论】:

    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 2015-07-11
    • 2014-12-23
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2017-09-23
    • 2018-05-04
    相关资源
    最近更新 更多