1

I have been trying to reproduce a Leaflet map on a Shiny webpage. I have made this code so far:

library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(dplyr)
library(DT)

Map <- readOGR("C:\\Users\\XXXXX\\Documents\\NationalElectorateMap\\NationalElectorateMap\\national-esri-fe2019\\COM_ELB_region.shp")

webdata_visitors <- read.csv("C://Users//XXXXX//Desktop//webdata_visitors.csv")

webdata_visitors <- webdata_visitors[order(match(webdata_visitors$Electorate, Map$Elect_div)),]

bins <- c(0, 100, 500, 1000, 10000, 350000, 50000, 75000, 200000, 500000)
pal <- colorBin("RdYlBu", domain = webdata_visitors$user_id, bins = bins)

ui <- dashboardPage(
    skin = "blue",
    dashboardHeader(title = "Users"),
    dashboardSidebar(),
    dashboardBody(
    fluidRow(width = 12, box(leafletOutput(outputId = "mymap")))))


server <- function(input, output) {

    data_input <- reactive({input$webdata_visitors})

    labels <- reactive({
        paste("<p>", data_input()$Electorate,"</p>",
                    "<p>", "Number of Visitors: ", round(data_input()$user_id, digit = 0), "</p>",
                    sep = "")
    })

    output$mymap <- renderLeaflet({
        ElectMap <- leaflet()%>%
            addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png') %>%
            setView(-25.2744, 133.7751, 4) %>%
            addPolygons(data=Map,
                        weight=1,
                        smoothFactor = 0.5,
                        color = "white",
                        fillOpacity = 0.8,
                        fillColor = pal(data_input()$user_id
                        ),
                        label = lapply(labels(), HTML)) %>%
            addLegend(pal = pal,
                      values = data_input()$user_id,
                      opacity = 0.7,
                      title = "Number or Visitors",
                      position = "topright")
       ElectMap })
    }

shinyApp(ui=ui,server=server)

However, when I run this code I see this error:

Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'list' [No stack trace available]

I am having trouble interpreting what this error means and I am not sure how to fix my code. Could anyone help me out?

1 Answer 1

0

I had a similar issue with creating a leaflet map within a shinydashboard. I could pin down the problem to the "label" argument of my circle markers. In your case that should be your addPolygons(..., label = lapply(labels(), HTML)). I added two groups of circle markers and while the first group delivered the error, the second group worked just fine. Apparantly this error message comes when the label argument doesn't find the data provided (see https://github.com/rstudio/leaflet/issues/617). So I think it's just a bug and i found a simple fix for me:

"tb()$Station" is just a character vector from a reactive table. addCircleMarkers(..., label = paste0("",tb()$Station))

I think just calling the label with paste0 before passing it to addCircleMarkers fixed the bug. Maybe you can add the "HTML" part to the reactive

    labels <- reactive({
    HTML(paste("<p>", data_input()$Electorate,"</p>",
                "<p>", "Number of Visitors: ", round(data_input()$user_id, digit = 0), "</p>",
                sep = ""))
})

and then addPolygons(..., labels = paste0("",labels()))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.