Hello,
I am working on a straight forward US elections dashboard. The particular issue I'm stuck on involves a slider that controls a bar chart created with ggplot. The dashboard displays without errors, but I have 2 issues:
1.) The axis shows the column header names instead of the continuous variable.
2.) The slider bar is not updating the bar chart.
I have no idea why this is happening. Here is a reproducible example:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(plotly)
year <- c(1960,1964,1968,1972,1976,1980,1984,1988,1992,1996,2000,2004,2008,2012,2016,2020)
dem_ev <- c(303,486,191,17,297,49,13,111,370,379,266,251,365,332,227,306)
rep_ev <- c(219,52,301,520,240,489,525,426,168,159,271,286,173,206,304,232)
df <- data.frame(cbind(year,dem_ev,rep_ev))
# Define UI for application
ui <- fluidPage(
dashboardPage(
dashboardHeader(title=""),
dashboardSidebar(
sidebarMenu(
menuItem("National Results",
tabName = "nat_results",
icon=icon("flag-usa")
)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "nat_results",
fluidRow(
sliderInput("slider",
"Choose an election year:",
min=1960, max=2020, step=4,value=2020,sep=""
)
),
fluidRow(
align='center',
plotlyOutput("elec_votes_bar")
)
)))))
# Define server logic
server <- function(input, output) {
filtered_data=reactive({
filter=subset(df,year==input$year_select)
return(filter)
})
output$elec_votes_bar <-renderPlotly({
p<-ggplot(data=filtered_data(), aes(x=c('Republican','Democrat'),
y=c("Rep_EV","Dem_EV"),
fill=c("#999999", "#E69F00"))) +
geom_bar(stat="identity")+
coord_flip()+
theme(legend.position="none")+
ylab('Electoral Votes')+xlab('')
p
})
}
# Run the application
shinyApp(ui = ui, server = server)
Can someone help point me in the right direction? Thank you!