根据R shine中的复选框选择创建打印



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

您可以使用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)

最新更新