【问题标题】:create plots based on check box selection in R shiny根据 R Shiny 中的复选框选择创建绘图
【发布时间】:2018-12-20 11:26:19
【问题描述】:

如何根据复选框输入创建动态图,图的数量应根据所选复选框的名称增加和减少。

【问题讨论】:

  • 请阅读How to make a great R reproducible example?并根据它编辑您的帖子。现在这个问题太宽泛了。
  • 请提供代码截图为您提供答案
  • 上面提供的链接与 R 相关,我正在寻找有关 R 闪亮的建议。你能提供任何与 R Shiny 相关的类似主题吗???

标签: r shiny visualization


【解决方案1】:

您可以使用library(gridExtra) 并根据您激活的复选框数量(grid.arrange)绘制多个图。

library(shiny)

# Define UI for application
ui <- fluidPage(
  checkboxGroupInput("variable", "Variables to show:",
                     c("Cylinders" = "cyl",
                       "Transmission" = "am",
                       "Gears" = "gear")),
  mainPanel(plotOutput("distPlot"))
)


# Define server logic
server <- function(input, output, session) {
  require(gridExtra)
  require(ggplot2)

  output$distPlot <- renderPlot({
    if (length(input$variable) == 0) {
      ggplot(data.frame())
    } else {
      gl <- lapply(input$variable, 
                   function(x) ggplot(mtcars, aes(mtcars[, x])) +
                     geom_bar() +
                     xlab(x)) 
      grid.arrange(grobs = gl, nrow = 1)
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2021-12-24
    • 2020-06-27
    • 2016-12-21
    相关资源
    最近更新 更多