1

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)

sp29
  • 363
  • 4
  • 11
  • Hi, there is no need for `reactive_option()$opt`, `reactive_option()` is enough. Same thing for `reactive_df()$manipulated_df` – bretauv May 29 '20 at 17:47
  • The code is running fine, but there is no display of plot – sp29 May 29 '20 at 18:17
  • I think you don't need the line with `isolate`. Remove it and try `output$plot <- renderPlot({ggplot(reactive_df())})` but don't forget to use `aes` in `ggplot` and to add `geom_line`, or `geom_point` or something else – bretauv May 29 '20 at 19:05
  • Yes, I have tried. And attached the actual code(drive link) in the question. It would be really helpful if you can give it a read. – sp29 May 29 '20 at 19:09
  • I have edited my answer – bretauv May 29 '20 at 20:18
  • Thank you for the help! – sp29 May 29 '20 at 21:00

1 Answers1

1

Your example is not reproducible because there is no data and no library calls. Please see here and here to know how to make a minimal, reproducible example.

Edit following OP's edit:

The problem is that you try to return different dataframes from reactive_df, which is impossible. You should instead merge covid_map and state_names together, so that all the information you need is in map.df, which is returned by reactive since it is the last action. Then, you can remove the long expressions in ggplot.

There was also a problem with the choices in radioButtons. It is better to only put the column names as choices and then to call filter the data with these column names with state[[input$radio_option]].

Here's your code:

# Loading libs
library(shiny)
library(shinythemes)
library(ggplot2)
library(mapdata)
library(mapproj)
library(data.table)
library(dplyr)

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")

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" = "TOTAL.CASES", "New Cases" = "NEW.CASES",
            "Active Cases" = "ACTIVE.CASES", "Total Deaths" = "TOTAL.DEATHS",
            "New Deaths" = "NEW.DEATHS", "Total Tests" = "TOTAL.TESTS"
          ),
          selected = "TOTAL.CASES"
        )
      ),
      mainPanel(plotOutput("us_cases"))
    )
  )
) # close fluid page

server <- function(input, output) {

  reactive_df <- reactive({
    us_geo_data <- data.table(us_geo_data)
    covid_map <- data.frame(
      state_names = unique(us_geo_data$region),
      values = state[[input$radio_option]]
    )
    setkey(us_geo_data, region)
    covid_map <- data.table(covid_map)
    setkey(covid_map, state_names)
    map.df <- dplyr::left_join(us_geo_data, covid_map, by = c("region" = "state_names"))
  })
  output$us_cases <- renderPlot(
    ggplot(
      reactive_df(),
      aes(
        x = long, y = lat,
        group = group, fill = values
      )
    ) +
      geom_polygon(alpha = 0.8) +
      coord_map()
  )
}

shinyApp(ui, server)
bretauv
  • 7,756
  • 2
  • 20
  • 57