用户选择/多输入的多个独立图

问题描述

我想根据用户输入可视化绘图。我有一个下拉菜单,如果用户从给定的选项中选择一个或多个变量,代码会根据用户输入自动可视化每个变量的单独图。

代码:这就是我尝试这样做的方式。如果有其他方法,请提出。

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 而不是 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))
    })
    
  }
)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...