Leaflet: Leaflet map not rendering in RStudio Viewer

Created on 6 Oct 2015  Â·  44Comments  Â·  Source: rstudio/leaflet

When I run the very simple example shown on the R Leaflet tutorial, I just get a grey screen with the zoom buttons. Here is the code I'm running:

map <- leaflet() %>% addTiles()
map

I read through the closed issues and found one similar to my issue here: https://github.com/rstudio/leaflet/issues/112 However, updating htmlwidgets, which I assume is the only library I'd need to worry about, didn't work. When I click "View in new window", the map displays fine. In addition, if I htmlwidgets::saveWidget(map, file="map.html"), the file saves fine and can be opened with my web browsers. I'm not sure why it won't display in the RStudio Viewer though. Any suggestions would be greatly appreciated.

> devtools::session_info()
Session info -------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.2 (2015-08-14)
 system   x86_64, mingw32             
 ui       RStudio (0.99.441)          
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2015-10-06                  

Packages -----------------------------------------------------------------------------------------------------------------------------------------------------
 package     * version    date       source                               
 devtools      1.9.1      2015-09-11 CRAN (R 3.2.2)                       
 digest        0.6.8      2014-12-31 CRAN (R 3.2.2)                       
 htmltools     0.2.6      2014-09-08 CRAN (R 3.2.2)                       
 htmlwidgets * 0.5.1      2015-10-06 Github (ramnathv/htmlwidgets@ee9864c)
 jsonlite      0.9.17     2015-09-06 CRAN (R 3.2.2)                       
 leaflet     * 1.0.1.9000 2015-10-06 Github (rstudio/leaflet@3b4d8a3)     
 magrittr      1.5        2014-11-22 CRAN (R 3.2.2)                       
 memoise       0.2.1      2014-04-22 CRAN (R 3.2.2)                       
 rstudioapi    0.3.1      2015-04-07 CRAN (R 3.2.2)                       
 yaml          2.1.13     2014-06-12 CRAN (R 3.2.2)

Most helpful comment

Here is actual addtile function, when you generate html file from htmlwidget, it create path as _file://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png_

function (map, urlTemplate = "//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", 
                      attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) 
{
    options$attribution = attribution
    if (missing(urlTemplate) && is.null(options$attribution)) 
        options$attribution = paste("&copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a>", 
                                    "contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>")
    invokeMethod(map, getMapData(map), "addTiles", urlTemplate, 
                 layerId, group, options)
}

FInally I solved simillar issue by changing function parameter as :

addTiles = function (map, urlTemplate = "**http:**//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution = NULL, layerId = NULL, group = NULL, options = tileOptions())
{
options$attribution = attribution
if (missing(urlTemplate) && is.null(options$attribution))
options$attribution = paste("© <a href="http://openstreetmap.org\">OpenStreetMap",
"contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA")
invokeMethod(map, getMapData(map), "addTiles", urlTemplate,
layerId, group, options)
}

All 44 comments

Have you tried zooming, sometimes I find that the tiles are all grey at first and with a zoom they show up. Not ideal, but a workaround.

The same issue here!
It plots the polygons, or markers I place, but no map. I work also on a windows machine.
When I try exactly the same code on a linux machine it works flawless. Any ideas?

Same issue

When teaching a recent workshop on this I found that users with older versions of RStudio were the ones having trouble. A version that works for me is 0.99.489.

Also, this is not a solution but a workaround, you can use options(viewer = NULL) and RStudio will send the map directly to a browser.

Additional info on this behavior is observed on Windows (Win 7), but I don't see this behavior on MacOS 10.X++ (Yosemite). Map Tiles appear fine on MacOS.

Same issue here. I was viewing maps just fine in RStudio viewer and now it saves a html map. In addition that html map did not render layers that were working when it was showing up in RStudio ("error is map$x: $ operator is invalid for atomic vectors"). I think it might have something to do with using geoJSON. After I played around with geoJSON formats and now have gone back to spatialXXdataframes this issue popped up. Any solutions?

.....30 seconds later.....

Well all I had to do was ask to figure out my own problem. I had loaded the leafletR package to convert spatialXXdataframes to geoJSONs (which didnt really work) which appeared to mask the normal leaflet() command. I tried re running all my code without leafletR loaded and my map came up in RStudio like it was doing.

The same issue here on Windows 7. I have no problem with plotting a few states at tract level, but RStudio stops work if I render all states at once. No problem with my Mac.

@Huade Could you provide a reproducible sample code ? Also details of R and RStudio versions please.

@bhaskar-vk Of course!

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sp_1.2-3

loaded via a namespace (and not attached):
 [1] htmlwidgets_0.6  colorspace_1.2-6 scales_0.4.0     plyr_1.8.4       magrittr_1.5     leaflet_1.0.1    htmltools_0.3.5 
 [8] tools_3.3.0      Rcpp_0.12.6      grid_3.3.0       digest_0.6.9     munsell_0.4.3    lattice_0.20-33 
Warning message:
package ‘sp’ was built under R version 3.3.1 
library(haven)
library(dplyr)
library(leaflet)
library(rgdal)
library(tigris)

# Read school district finance data
# https://nces.ed.gov/ccd/f33agency.asp
# https://nces.ed.gov/ccd/data/zip/sdf13_1a.sas7bdat.zip

options(stringsAsFactors = F)
ccd_raw <- read_sas("./data/sdf13_1a.sas7bdat")

ccd <- ccd_raw %>% 
  mutate(property_tax = ifelse(T06>=0, T06, NA),
         total_local_revenue = ifelse(TLOCREV >= 0, TLOCREV, NA),
         pct_property_tax = 100*property_tax / total_local_revenue) %>% 
  select(LEAID, pct_property_tax) %>% 
  filter(!is.na(pct_property_tax))

# Read School District Geographic Relationship Files
# https://nces.ed.gov/programs/edge/geographicRelationshipFiles.aspx
# https://nces.ed.gov/programs/edge/data/GRF15.zip
lea_geo_link <- read_sas("./data/grf15_lea_tract.sas7bdat") %>% 
  select(LEAID, GEOID = STCOTRACT)

# Read School District Boundaries
# https://nces.ed.gov/programs/edge/geographicDistrictBoundary.aspx
# https://nces.ed.gov/programs/edge/data/SCHOOLDISTRICT_SY1314_TL15.zip

school_dist_boundary <- readOGR("./shp/schooldistrict_sy1314_tl15.shp",
                                layer = "schooldistrict_sy1314_tl15", verbose = FALSE)

pct_property_tax_merged_full <- geo_join(school_dist_boundary, ccd, "GEOID", "LEAID")
pct_property_tax_merged <- pct_property_tax_merged_full[pct_property_tax_merged_full$STATEFP %in% unique(pct_property_tax_merged_full$STATEFP)[50],]

popup <- paste0("LEAID: ", pct_property_tax_merged$LEAID, "<br>", 
                pct_property_tax_merged$NAME,  "<br>",
                "Percent of Property Tax as Local Revenue: ", round(pct_property_tax_merged$pct_property_tax,2), "%")
pal <- colorNumeric(
  palette = "YlGnBu",
  domain = pct_property_tax_merged$pct_property_tax
)

map1<- leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(data = pct_property_tax_merged, 
              fillColor = ~pal(pct_property_tax), 
              color = "#b2aeae", 
              fillOpacity = 0.7, 
              weight = 1, 
              smoothFactor = 0.2,
              popup = popup) %>%
  addLegend(pal = pal, 
            values = pct_property_tax_merged$pct_property_tax, 
            position = "bottomright", 
            title = "Percent of Property Tax as Local Revenue",
            labFormat = labelFormat(suffix = "%")) 

map1

Thanks @Huade I'll need the data files as well to try and reproduce it here.

I have updated my code and included download addresses, @bhaskarvk . You may want to try visit the webpage (and accept their terms) if the direct download does not work well. Thanks for reproducing!

+1 thanks I'll take a look at this sometime this week.

any update on possible remedial steps for this issue?

I get the same behaviour using RStudio 0.99.903 on Windows 10 whereby the map itself doesn't render; rather I only see the grey background.

+1 with this issue!

@robsalasco @Huade @gfcronin Are you guys by any chance behind a web proxy ?

I know you didn't direct the comment at me, but yes, I am.

Yes; Windows 7 is my office computer. Actually, the issue still persists if I run Rstudio on EC2 on this Windows 7 computer.

@bhaskarvk Any news on this? I have the same issue and yes, I am behind a web proxy.

@jcheng5 wasn't this fixed in one of the recent RStudio versions ?

@bhaskarvk @jcheng5 . Would be great if one of the recent RStudio versions solved the problem. We're currently using a slightly older version (0.99.879), which should be updated shortly.
Many thanks for your reply.

FWIW I am not officially involved with RStudio so I can't comment in any official capacity, but I seem to remember this issue being fixed from some discussion I had seen.

Is there a way around this bug? Demonstrable both on the R command line and in Rstudio (nightly build, linux 64 ubuntu1.1.75)

[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] feather_0.3.1 ggmap_2.6.1 leafletR_0.4-0 leaflet_1.0.2.9010
[5] dplyr_0.5.0 purrr_0.2.2 readr_1.0.0 tidyr_0.6.1
[9] tibble_1.2 ggplot2_2.2.1 tidyverse_1.1.1

loaded via a namespace (and not attached):
[1] Rcpp_0.12.9 plyr_1.8.4 forcats_0.2.0 tools_3.3.2
[5] digest_0.6.12 jsonlite_1.2 lubridate_1.6.0 gtable_0.2.0
[9] nlme_3.1-130 lattice_0.20-34 png_0.1-7 psych_1.6.12
[13] DBI_0.5-1 mapproj_1.2-4 parallel_3.3.2 haven_1.0.0
[17] proto_1.0.0 stringr_1.1.0 httr_1.2.1 xml2_1.1.1
[21] maps_3.1.1 RgoogleMaps_1.4.1 htmlwidgets_0.8 hms_0.3
[25] grid_3.3.2 data.table_1.10.4 R6_2.2.0 jpeg_0.1-8
[29] readxl_0.1.1 foreign_0.8-67 sp_1.2-4 reshape2_1.4.2
[33] modelr_0.1.0 magrittr_1.5 scales_0.4.1 htmltools_0.3.5
[37] assertthat_0.1 mnormt_1.5-5 rvest_0.3.2 geosphere_1.5-5
[41] colorspace_1.3-2 brew_1.0-6 stringi_1.1.2 lazyeval_0.2.0
[45] munsell_0.4.3 broom_0.4.1 rjson_0.2.15

Here is actual addtile function, when you generate html file from htmlwidget, it create path as _file://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png_

function (map, urlTemplate = "//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", 
                      attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) 
{
    options$attribution = attribution
    if (missing(urlTemplate) && is.null(options$attribution)) 
        options$attribution = paste("&copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a>", 
                                    "contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>")
    invokeMethod(map, getMapData(map), "addTiles", urlTemplate, 
                 layerId, group, options)
}

FInally I solved simillar issue by changing function parameter as :

addTiles = function (map, urlTemplate = "**http:**//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution = NULL, layerId = NULL, group = NULL, options = tileOptions())
{
options$attribution = attribution
if (missing(urlTemplate) && is.null(options$attribution))
options$attribution = paste("© <a href="http://openstreetmap.org\">OpenStreetMap",
"contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA")
invokeMethod(map, getMapData(map), "addTiles", urlTemplate,
layerId, group, options)
}

kgavhane702 thanks for the tip!! Absolutely works when setting http path in addTiles function

@kgavhane702 @ferjmele That bug has been fixed in the GitHub version of leaflet, though not re-released to CRAN yet. Though the fix by @kgavhane702 works well and should continue to work.

ok!! Thanks!

@ferjmele welcome.

Hi - I'm still experiencing this issue even so I use the leaflet package version of github and the lastest release of RStudio (Version 1.0.136).

Here is the code I'm testing:
`library(leaflet)

m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")

m # Print the map`

The code is running without error nor messages. It generates a map in the Rstudio viewer with a grey background and a marker but no tiles in the background. When I try to view the map in a browser (IE or Chrome), it succesfully shows the expected results.

Here are my session info:
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] leaflet_1.1.0.9000

loaded via a namespace (and not attached):
[1] htmlwidgets_0.8 shiny_1.0.0 magrittr_1.5 R6_2.2.0 htmltools_0.3.5
[6] tools_3.3.3 yaml_2.1.14 Rcpp_0.12.9 crosstalk_1.0.0 jsonlite_1.3
[11] digest_0.6.12 xtable_1.8-2 httpuv_1.3.3 mime_0.5

We also tested with no success:

  • the current version of the leaflet package on CRAN.
  • the overloading of the addTiles function.

Thx for your help

@Mamasquee Do you get the same results with a 1.1 build of RStudio (https://dailies.rstudio.com)?

thanks @jcheng5
unfortunately still not working with Version 1.1.121 – © 2009-2016 RStudio, Inc.
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) rstudio Safari/538.1 Qt/5.4.1.
can I test something else maybe ?

@Mamasquee It's a different bug then. See this one: https://github.com/rstudio/leaflet/issues/240

Thanks @kgavhane702

Thanks @kgavhane702

Thanks @kgavhane702. Great tip. I was getting a grey screen with no tiles in my browser with leaflet on my Mac but when I added "http" to the urlTemplate in addTiles, everything works.

library (leaflet)
mymap <- leaflet()

mymap <- addTiles(mymap) # grey screen, no tiles

mymap <- addTiles(mymap, urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") # yey! OpenStreetMap tiles!
mymap

Welcome

@kgavhane702 I used your code but it still didn't work. The image I got is like this
screen shot 2018-03-19 at 2 57 14 pm

Hey @eddiecylin leaflet community has solved similar issue on GitHub. download GitHub version of leaflet, and you will definitely overcome with this error.

if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')

Hi @kgavhane702, I tried but I am sorry that it still didn't work out. I even switched another computer and installed RStudio and used devtools::install_github('rstudio/leaflet'). But still didn't work out.

@eddiecylin What operating system, and version of RStudio? And are you on a corporate network (especially one with some kind of proxy setup)?

@jcheng5 @kgavhane702 thanks for your help. I think I figured out why. I made a mistake by putting longitude and latitude parameters in the wrong order. So my map of Manhattan always showed up next to Brazil and had to zoom in all the way to show. I made a correction and set the zoom= properly, and the map showed nicely. Thanks!

Great..!

For me (Ubuntu 18.04) works in browser but not in RStudio

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2       leaflet_2.0.2        sf_0.7-1             gpclib_1.5-5         maptools_0.9-4       sp_1.3-1            
 [7] rgeos_0.4-2          broom_0.5.1          tigris_0.7           lubridate_1.7.4      shinydashboard_0.7.1 plotly_4.8.0        
[13] tm_0.7-5             NLP_0.2-0            wordcloud_2.6        RColorBrewer_1.1-2   plyr_1.8.4           forcats_0.3.0       
[19] stringr_1.3.1        purrr_0.2.5          readr_1.1.1          tidyr_0.8.1          tibble_1.4.2         ggplot2_3.1.0       
[25] tidyverse_1.2.1      dplyr_0.7.8          shiny_1.2.0         

loaded via a namespace (and not attached):
 [1] httr_1.3.1        jsonlite_1.5      viridisLite_0.3.0 modelr_0.1.2      assertthat_0.2.0  cellranger_1.1.0  yaml_2.2.0       
 [8] slam_0.1-43       pillar_1.3.0      backports_1.1.2   lattice_0.20-35   glue_1.3.0        uuid_0.1-2        digest_0.6.18    
[15] promises_1.0.1    rvest_0.3.2       colorspace_1.3-2  htmltools_0.3.6   httpuv_1.4.5      pkgconfig_2.0.2   haven_1.1.2      
[22] xtable_1.8-3      scales_1.0.0      later_0.7.5       generics_0.0.2    withr_2.1.2       lazyeval_0.2.1    cli_1.0.0        
[29] magrittr_1.5      crayon_1.3.4      readxl_1.1.0      mime_0.5          fansi_0.3.0       nlme_3.1-131      xml2_1.2.0       
[36] foreign_0.8-69    class_7.3-14      tools_3.4.4       data.table_1.11.8 hms_0.4.2         munsell_0.5.0     compiler_3.4.4   
[43] e1071_1.7-0       rlang_0.3.0.1     classInt_0.2-3    units_0.6-1       grid_3.4.4        rstudioapi_0.7    rappdirs_0.3.1   
[50] htmlwidgets_1.3   crosstalk_1.0.0   labeling_0.3      gtable_0.2.0      DBI_1.0.0         R6_2.3.0          rgdal_1.3-4      
[57] utf8_1.1.4        bindr_0.1.1       stringi_1.2.4     parallel_3.4.4    Rcpp_1.0.0        spData_0.2.9.6    tidyselect_0.2.5 

Locking conversation as it is old.

Please install a daily build of RStudio (v1.2) if you are experiencing https issues when loading map tiles with leaflet.

If this is not related to loading https on windows and linux, please file a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jz1584 picture jz1584  Â·  3Comments

Make42 picture Make42  Â·  7Comments

prateekjoshi565 picture prateekjoshi565  Â·  3Comments

gdmcdonald picture gdmcdonald  Â·  4Comments

loerasg picture loerasg  Â·  6Comments