Search code examples
rshinyshinydashboardgooglevis

Reactive function is not working for creating Choropleth Map


I'm trying to create a choropleth map using googleVis in shiny. When I create the reactive function that selects specific data according to the input I get the error:

Error in as.vector: cannot coerce type 'closure' to vector of type 'list'

The data used is a sample of the original dataset.

Can anybody help me to solve this problem?

df <- data.frame(GEO = c("Belgium", "Germany" , 
                         "France","Italy","Sweden","Slovakia" ),
                 PRODUCT = c("Total petroleum products","Gas"),
                 TIME = c(1990),
                 Value = c(18345, 126544, 88659,90069,14670,4974), 
                 stringsAsFactors = FALSE)


library(shiny)
library(shinydashboard)
library(googleVis)

ui <- fluidPage(titlePanel("Energy consumption in Europe"),

                dashboardPage(
                  dashboardHeader(),
                  dashboardSidebar(width = 240,
                                   br(),
                                   br(),
                                   br(),
                                   selectizeInput("Type",
                                                  label = em("Select Type", style="text-align:center;color:#FFA319;font-size:150%"),
                                                  choices = unique(df$PRODUCT),
                                                  selected = "")),
                  dashboardBody(htmlOutput("firstMap"))
                ))

server <- function(input, output) {

  data_selected<-reactive({
    df[df$PRODUCT%in%input$Type]
  })


  output$firstMap <- renderGvis({
    data(data_selected)
    map<-gvisGeoChart(data_selected,"GEO", "Value",
                      options=list(region="150"))


    return(map)
  })
}

shinyApp(ui, server)

Solution

  • server <- function(input, output) {
    
    data_selected<-reactive({
    df[df$PRODUCT%in%input$Type,]
     })
    
    
     output$firstMap <- renderGvis({
     req(data_selected())
     map<-gvisGeoChart(data_selected(),"GEO", "Value",
                      options=list(region="150"))
    
    
         return(map)
     })
    }
    
    shinyApp(ui, server)
    

    Try this one.