Error in sum: invalid 'type' (list) of argument

Hi All,

I badly need your help in getting this resolved as I am struggling with this for weeks.

I copied the code for "Displaying shapes with custom projections" from the below link:

https://rstudio.github.io/leaflet/projections.html

library(leaflet)
library(sp)
library(albersusa)

spdf <- rmapshaper::ms_simplify(usa_sf(), keep = 0.1)
pal <- colorNumeric("Blues", domain = spdf$pop_2014)
epsg2163 <- leafletCRS(
  crsClass = "L.Proj.CRS",
  code = "EPSG:2163",
  proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
  resolutions = 2^(16:7))

leaflet(spdf, options = leafletOptions(crs = epsg2163)) %>%
  addPolygons(weight = 1, color = "#444444", opacity = 1,
    fillColor = ~pal(pop_2014), fillOpacity = 0.7, smoothFactor = 0.5,
    label = ~paste(name, pop_2014),
    labelOptions = labelOptions(direction = "auto"))

The above code displays the map with tool tips.

filteredData2 <- reactive({
                spdf[spdf$MMWRyear == input$MMWRyear1[1] & spdf$population == input$population1[1] & spdf$MMWRweek == input$slider2[1],]
            })

When I try to add a reactive feature (above code) with drop downs and sliders and when I try to pass "filteredData2()" in place of spdf in the line "leaflet(spdf, options = leafletOptions(crs = epsg2163)) %>%" in the map code I am getting the error "Error in sum: invalid 'type' (list) of argument". I was able to add the drop downs to a different leaflet map and that was working map. But this one is giving me this error. I am clueless on what needs to be done.

Need someones help.

Thanks in advance.

Hello, I'd like to help you.
I dont understand your data though.
Your working example above works on an spdf object with the following named contents:

names(spdf)
 [1] "geo_id"              "fips_state"          "name"                "lsad"               
 [5] "census_area"         "iso_3166_2"          "census"              "pop_estimataes_base"
 [9] "pop_2010"            "pop_2011"            "pop_2012"            "pop_2013"           
[13] "pop_2014"            "geometry"           

your filterData2 requires your spdf to contain MMWRyear, population, and MMWRweek do these items exist for you ?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

Hi Nir A Graham,

Thanks for your reply. I have already modified the spdf Dataframe. Kindly see below the columns available in the Dataframe. MMWRyear, population, and MMWRweek are present.

names(spdf)
[1] "name" "MMWRyear" "MMWRweek" "population" "coverage" "State" "Latitude" "Longitude" "geometry"

I have included the filters for MMWRyear, population, and MMWRweek as follows:

selectInput(inputId = "MMWRyear1", "Select the Year", c("All", unique(as.character(spdf$MMWRyear))), selected = "2016", multiple = TRUE),

selectInput(inputId = "population1", "Select the Age Group", c("All", unique(as.character(spdf$population))), selected = "Total population", multiple = TRUE)

and for the slider:

tabItem("dashboard2", label = "Leaflet Map 2",
leafletOutput("mymap2"),
p(),
sliderInput("slider2", "WEEK",
min = 1, max = 52, value = 1, step = 1, animate =
animationOptions(interval = 1000, loop = TRUE))
)

Thanks in advance.

Now I am getting this error:

don't know how to get path data from object of class data.frame

I'm afraid that in order to help you , I would need a reprex. As I'm unable to recreate your issue without it.

I took the code and initial data that you originally provided, and satisfied myself that there is nothing inherintly wrong with your apprpoach, it works after all. I suppose the devil is in the details which I can't access as of yet.

Here is that test shiny app I created.
The top plot is unfiltered, the bottom one is filtered on State code (iso..)


library(shiny)
library(leaflet)
library(sp)
library(albersusa) #devtools::install_github("hrbrmstr/albersusa")
library(rmapshaper)

spdf <- rmapshaper::ms_simplify(usa_sf(), keep = 0.1)
pal <- colorNumeric("Blues", domain = spdf$pop_2014)
epsg2163 <- leafletCRS(
  crsClass = "L.Proj.CRS",
  code = "EPSG:2163",
  proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
  resolutions = 2^(16:7))

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
 
  titlePanel("leaflettest"),
  sidebarLayout(sidebarPanel (
    selectInput(inputId = "iso_3166_2",label="Filter on iso_3166_2",
                choices = unique(pull(spdf,iso_3166_2)))
  ),
              mainPanel (  
  leafletOutput("leafplot1"),
  leafletOutput("leafplot2")
              )
))

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  unfilteredData1 <- reactive({
   spdf
  })
  filteredData2 <- reactive({
    spdf[spdf$iso_3166_2 == input$iso_3166_2,]
  })

  output$leafplot1 <- renderLeaflet({
    
    leaflet(unfilteredData1(), options = leafletOptions(crs = epsg2163)) %>%
      addPolygons(weight = 1, color = "#444444", opacity = 1,
                  fillColor = ~pal(pop_2014), fillOpacity = 0.7, smoothFactor = 0.5,
                  label = ~paste(name, pop_2014),
                  labelOptions = labelOptions(direction = "auto"))
  })
  output$leafplot2 <- renderLeaflet({
    
    leaflet(filteredData2(), options = leafletOptions(crs = epsg2163)) %>%
      addPolygons(weight = 1, color = "#444444", opacity = 1,
                  fillColor = ~pal(pop_2014), fillOpacity = 0.7, smoothFactor = 0.5,
                  label = ~paste(name, pop_2014),
                  labelOptions = labelOptions(direction = "auto"))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Hey Nirgrahamuk,

Kudos to you. I got it working man. Thanks for providing the small piece of interactive code as an example. I was able to incorporate that to the bigger dataset.

Thank you so much for helping me out.

1 Like

Hi Nirgrahamuk,

Will you be able to provide some advice on the below question?

Thanks in advance.