如何根据复选框的输入创建动态图,应相对于选中的名称来增加或减少图的数量。
答案 0 :(得分: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)