R语言中shiny如何根据表格画出三类元素的柱状图并输出到界面,给出代码样例
时间: 2024-05-14 08:12:38 浏览: 100
以下是用shiny根据表格画出三类元素的柱状图的代码样例:
```
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
titlePanel("柱状图"),
sidebarLayout(
sidebarPanel(
selectInput("select", label = "选择表格:", choices = c("Table1", "Table2", "Table3"))
),
mainPanel(
plotOutput("barplot")
)
)
)
server <- function(input, output) {
data <- reactive({
switch(input$select,
"Table1" = iris %>% group_by(Species) %>% summarise(mean = mean(Sepal.Length)),
"Table2" = iris %>% group_by(Species) %>% summarise(mean = mean(Petal.Length)),
"Table3" = iris %>% group_by(Species) %>% summarise(mean = mean(Sepal.Width))
)
})
output$barplot <- renderPlot({
ggplot(data(), aes(x=Species, y=mean, fill=Species)) + geom_bar(stat="identity")
})
}
shinyApp(ui = ui, server = server)
```
希望可以帮助到你!现在,你可以继续问我其它问题了。
阅读全文