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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.