sp29
sp29

Reputation: 383

Reactive function giving error in Shiny framework R

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)

Upvotes: 1

Views: 173

Answers (1)

bretauv
bretauv

Reputation: 8506

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)

Upvotes: 1

Related Questions