I am trying to make a reactive plot with radio buttons in shiny. However, after using the reactive expressions I am not able to see any plot.
Here is the reproducible code of the complete app
# Loading libs
library(shiny)
library(shinythemes)
library(ggplot2)
library(mapdata)
library(mapproj)
ui <- fluidPage(theme = shinytheme("cosmo"),
navbarPage("PageTitle",
tabPanel("US-Data-Visualizer",
sidebarPanel(tags$h2("Map Visualizer"), tags$h4("Visualize the counts on US map, state wise"),
radioButtons("radio_option", label = "Select from the following options:",
choices = list("Total Cases" = "state$TOTAL.CASES", "New Cases" = "state$NEW.CASES",
"Active Cases" = "state$ACTIVE.CASES", "Total Deaths" = "state$TOTAL.DEATHS",
"New Deaths" = "state$NEW.DEATHS", "Total Tests" = "state$TOTAL.TESTS"),
selected = "state$TOTAL.CASES")),
mainPanel(plotOutput("us_cases"))))
) #close fluid page
server <- function(input, output){
state <- read.csv("https://raw.githubusercontent.com/spriyansh/ShinyApps/master/datasets/USA_State_Wise_Data.csv", fileEncoding="UTF-8")
us_geo_data <- map_data("county")
reactive_df <- reactive({
us_geo_data <- data.table(us_geo_data)
covid_map <- data.frame(state_names=unique(us_geo_data$region),
values = input$radio_option)
setkey(us_geo_data,region)
covid_map <- data.table(covid_map)
setkey(covid_map,state_names)
map.df <- us_geo_data[covid_map]})
output$us_cases <- renderPlot(
ggplot(reactive_df()$map.df,
aes(x = reactive_df()$map.df$long, y = reactive_df()$map.df$lat,
group = reactive_df()$map.df$group,fill = reactive_df()$covid_map$values)) +
geom_polygon(alpha = 0.8) + coord_map()
)}
shinyApp(ui, server)