您可以使用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)
Run Code Online (Sandbox Code Playgroud)