Shiny - cannot read data inside reactive function

shiny
leaflet

#1

Everything is reproducible, including the data to plot

I'm ploting a basic map, but data is not being read by server.R:

Its not loading a geojson file I'm using to plot the map, I need to manually Ctrl + Enter this line to make it work:

If not, R tells that couldn't find object peru:


       peru <- geojsonio::geojson_read("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson", what = "sp")
      

What could be wrong?

server.R:


library(shiny)
library(dplyr)
library(DT)
require(leaflet)
library(readr)




ab <- reactive({
     
      
      ventas_agosto_16 <- data.frame("FECHA_PED" = 1:25,  "DEPARTAMENTO" = c("LIMA", "AREQUIPA", "LA LIBERTAD",
                                                                             "MOQUEGUA","HUANUCO","CUSCO",        
                                                                             "PIURA","CALLAO","CAJAMARCA",    
                                                                             "UCAYALI","JUNIN","ICA",          
                                                                             "LAMBAYEQUE","AMAZONAS","ANCASH",       
                                                                             "AYACUCHO","LORETO","SAN MARTIN",   
                                                                             "TACNA","MADRE DE DIOS","PUNO",         
                                                                             "TUMBES","PASCO","APURIMAC",     
                                                                             "HUANCAVELICA"),
                                     "TOTAL_PEDIDO" = runif(n = 25, min = 2, max = 25))
      
  
      peru <- geojsonio::geojson_read("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson", what = "sp")
      
      
      peru$ventas <- ventas_agosto_16$TOTAL_PEDIDO
      
      peru
      
})



server <- function(input, output) {
  



  
  
  output$mymap <- renderLeaflet({
    
    
    
  
    pal <- colorBin("YlOrRd", domain = peru$ventas, bins = 5)
    
    
    labels <- sprintf(
       "<strong>%s</strong><br/>%g soles",
       peru$NOMBDEP, peru$ventas
     ) %>% lapply(htmltools::HTML)
     
    
    
    leaflet(peru) %>%
      setView(-75, -10, 4) %>%
      addProviderTiles("MapBox", options = providerTileOptions(
        id = "mapbox.light",
        accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
      addPolygons(
        fillColor = ~pal(ventas),
        weight = 2,
        opacity = 1,
        color = "white",
        dashArray = "3",
        fillOpacity = 0.7,
        highlight = highlightOptions(
          weight = 3,
          color = "#666",
          dashArray = "",
          fillOpacity = 0.7,
          bringToFront = TRUE),
        label = labels,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "3px 8px"),
          textsize = "15px",
          direction = "auto")) %>%
      addLegend(pal = pal, values = ~ventas, opacity = 0.7, title = NULL,
                position = "bottomright")
    
  })
  
  
}

Thank you!


#2

Hi @o_gonzales, I am not able to run your app unfortunately, since there is no UI defined.

But, it seems to me a scoping issue. The peru variable is defined inside the reactive 'ab' and thus it's only available there. I guess what you want to do is call the reactive in your renderLeaflet, e.g.

  output$mymap <- renderLeaflet({
    peru <- ab()
    pal  <- colorBin("YlOrRd", domain = peru$ventas, bins = 5)