Search code examples
rshinyr-leaflet

Points data not found


I am trying to plot circles on leaflet following the example given in RSTUDIO'S github page here. I have been trying to get it work for the last couple of days be adopting various suggestions given here as well as in other blogs. But I am getting the following error constantly:

Warning: Error in derivePoints: Point data not found; please provide addCircles with data and/or lng/lat arguments

I am not sure if I am missing any libraries or if any of my packages requires update. The following is the data set along with the codes. If I run the example as is from the r-studio's github page, it runs without any issues. I check the structure of the data, and both are exactly the same types. Not sure where the issue could be:

Desired output: A map with differential sized circles for each category (cereals, pulses) which reactively changes upon selection.

library(shiny)
library(leaflet)
library(RColorBrewer)

data <- structure(list(LATITUDE = c(26.912434, 28.459497, 23.022505, 
10.790483, 28.704059), LONGITUDE = c(75.787271, 77.026638, 72.571362, 
78.704673, 77.10249), CEREALS = c(450L, 350L, 877L, 1018L, 600L
), PULSES = c(67L, 130L, 247L, 250L, 324L)), .Names = c("LATITUDE", 
"LONGITUDE", "CEREALS", "PULSES"), row.names = c(1263L, 4524L, 
10681L, 7165L, 12760L), class = "data.frame")

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
    selectInput(inputId = "productCategoryMonthly", "PRODUCTS",choices = NULL, selected = NULL),
      selectInput("colors", "Color Scheme",rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
    )))


server <- function(input, output, session) {

    df <- reactive({data})    

    observe({
        withProgress(message = "Loading data, Please wait...",value = 0.1, {
        updateSelectizeInput(session,inputId = "productCategoryMonthly", choices = as.character(sort(toupper(colnames(df()[,c(3:4)]))),decreasing = TRUE), selected = "CEREALS", server = TRUE)
        })
    })

  filteredData <- reactive({
        if(input$productCategoryMonthly == "CEREALS") {
            df()[,c(1,2,3)]
        } else if (input$productCategoryMonthly == "PULSES") {
                df()[,c(1,2,4)]
        } 
  })

  output$map <- renderLeaflet({
    leaflet(df()) %>% addTiles() %>%
      fitBounds(~min(LONGITUDE), ~min(LATITUDE), ~max(LONGITUDE), ~max(LATITUDE))
  })


  observe({
    mag <- filteredData()[[input$productCategoryMonthly]]
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addCircles(radius = ~10^mag/10, weight = 1, color = "#777777",
        fillOpacity = 0.7, popup = ~paste(mag)
      )
  })

}

shinyApp(ui, server)

Solution

  • This is a bit modified code:

    I have rescaled the mag variable to control the circle sizes, you might want to play with that so that the circle area represent the quantities regardless of the product category. I have hard-coded the product drop-down, the dynamic creation of the dropdown does not work. Will take a look at that later. The fill color was missing in the leafletProxy call. This is the code:

    library(shiny)
        library(leaflet)
        library(RColorBrewer)
    
    
    
        data <- structure(list(LATITUDE = c(26.912434, 28.459497, 23.022505, 
                                            10.790483, 28.704059), 
                               LONGITUDE = c(75.787271, 77.026638, 72.571362, 
                                                                                 78.704673, 77.10249), 
                               CEREALS = c(450L, 350L, 877L, 1018L, 600L
                                                                                 ), 
                               PULSES = c(67L, 130L, 247L, 250L, 324L)), 
                          .Names = c("LATITUDE", "LONGITUDE", "CEREALS", "PULSES"), 
                          row.names = c(1263L, 4524L, 10681L, 7165L, 12760L), 
                          class = "data.frame")
        #mag <- c(5, 5.2, 5.3, 5.4, 5.5)
    
        ui <- bootstrapPage(
                tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
                leafletOutput("map", width = "100%", height = "100%"),
                absolutePanel(top = 10, right = 10,
                              selectInput(inputId = "productCategoryMonthly", "PRODUCTS",choices = c("CEREALS", "PULSES"), selected = NULL),
                              selectInput("colors", "Color Scheme",rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                              )))
    
        server <- function(input, output, session) {
    
                df <- reactive({data})    
    
                # observe({
                #         withProgress(message = "Loading data, Please wait...",value = 0.1, {
                #                 updateSelectInput(session,inputId = "productCategoryMonthly", choices = as.character(sort(toupper(colnames(df()[,c(3:4)]))),decreasing = TRUE), selected = "CEREALS", server = TRUE)
                #         })
                # })
    
                filteredData <- reactive({
                        if(input$productCategoryMonthly == "CEREALS") {
                                df()[,c(1,2,3)]
                        } else if (input$productCategoryMonthly == "PULSES") {
                                df()[,c(1,2,4)]
                        } 
                })
                mag <- reactive({
                        if(input$productCategoryMonthly == "CEREALS") {
                                mag <- (filteredData()[[input$productCategoryMonthly]]/sum(filteredData()[[input$productCategoryMonthly]])) + 5       
                        } else if (input$productCategoryMonthly == "PULSES") {
                                mag <- (filteredData()[[input$productCategoryMonthly]]/sum(filteredData()[[input$productCategoryMonthly]])) + 5 
                        }    
                        mag
                })
    
                output$map <- renderLeaflet({
                        leaflet(df()) %>% addTiles() %>%
                                fitBounds(~min(LONGITUDE), ~min(LATITUDE), ~max(LONGITUDE), ~max(LATITUDE))
                })
    
                colorpal <- reactive({
                        colorNumeric(input$colors, filteredData()[[3]])
                })
                observe({
                        pal <- colorpal()
                        #mag <- filteredData()[[input$productCategoryMonthly]]^(1/4)
                        leafletProxy("map", data = filteredData()) %>%
                                clearShapes() %>%
                                addCircles(radius = ~10^mag()/10, weight = 1, color = "#777777", fillColor = ~pal(filteredData()[[3]]),
                                           fillOpacity = 0.7, popup = ~paste(mag())
                                )
                })
    
        }
    
        shinyApp(ui, server)