Reputation: 307
Based on my data, I can prepare a simple dodge bar plot
geo <- data.frame("year" = c(2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020),
"geo" = c("Europe", "Asia", "America", "Europe", "Asia", "America", "Europe", "Asia", "America"),
"sales" = c(100, 150, 200, 500, 500, 500, 1200, 1800, 1200))
ggplot(geo, aes(fill=as.factor(year), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
I want to put this plot in the Shiny dashboard, together with a checkbox to select years. I do
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
checkboxGroupInput("checkGroup", label = "Year",
choices = list("2018"=2018, "2019" = 2019, "2020" = 2020),
selected = 2018)
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(geo, aes(fill=as.factor(input$checkGroup), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
})
}
shinyApp(ui, server)
I get two types of errors:
Can anyone help?
Upvotes: 2
Views: 592
Reputation: 123978
This could be achieved like so:
reactive
.year
on fill
library(shiny)
library(shinydashboard)
geo <- data.frame("year" = c(2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020),
"geo" = c("Europe", "Asia", "America", "Europe", "Asia", "America", "Europe", "Asia", "America"),
"sales" = c(100, 150, 200, 500, 500, 500, 1200, 1800, 1200))
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
checkboxGroupInput("checkGroup", label = "Year",
choices = list("2018"=2018, "2019" = 2019, "2020" = 2020),
selected = 2018)
)
)
)
)
server <- function(input, output) {
dat <- reactive({
filter(geo, year %in% input$checkGroup)
})
output$plot1 <- renderPlot({
ggplot(dat(), aes(fill=as.factor(year), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
})
}
shinyApp(ui, server)
Upvotes: 2