Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaflet map not rendering in RStudio Viewer #192

Closed
ghost opened this issue Oct 6, 2015 · 44 comments
Closed

Leaflet map not rendering in RStudio Viewer #192

ghost opened this issue Oct 6, 2015 · 44 comments
Assignees

Comments

@ghost
Copy link

ghost commented Oct 6, 2015

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: #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)
@zross
Copy link

zross commented Dec 9, 2015

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.

@Marnick
Copy link

Marnick commented Jan 12, 2016

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?

@thohan88
Copy link

Same issue

@zross
Copy link

zross commented Jan 19, 2016

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.

@kkruups
Copy link

kkruups commented Apr 12, 2016

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.

@wraymond
Copy link

wraymond commented Jul 24, 2016

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.

@Huade
Copy link

Huade commented Sep 8, 2016

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.

@bhaskarvk
Copy link
Collaborator

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

@Huade
Copy link

Huade commented Sep 13, 2016

@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

@bhaskarvk
Copy link
Collaborator

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

@Huade
Copy link

Huade commented Sep 13, 2016

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!

@bhaskarvk
Copy link
Collaborator

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

@bhaskarvk bhaskarvk self-assigned this Sep 13, 2016
@gfcronin
Copy link

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.

@robsalasco
Copy link

+1 with this issue!

@bhaskarvk
Copy link
Collaborator

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

@ghost
Copy link
Author

ghost commented Oct 12, 2016

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

@Huade
Copy link

Huade commented Oct 12, 2016

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

@dragosmg
Copy link

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

@bhaskarvk
Copy link
Collaborator

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

@dragosmg
Copy link

dragosmg commented Jan 26, 2017

@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.

@bhaskarvk
Copy link
Collaborator

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.

@aetiologicCanada
Copy link

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

@kgavhane702
Copy link

kgavhane702 commented Feb 27, 2017

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)
}

@ferjmele
Copy link

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

@jcheng5
Copy link
Member

jcheng5 commented Mar 15, 2017

@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.

@ferjmele
Copy link

ok!! Thanks!

@kgavhane702
Copy link

kgavhane702 commented Mar 16, 2017

@ferjmele welcome.

@Mamasquee
Copy link

Mamasquee commented Mar 17, 2017

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

@jcheng5
Copy link
Member

jcheng5 commented Mar 17, 2017

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

@Mamasquee
Copy link

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 ?

@jcheng5
Copy link
Member

jcheng5 commented Mar 23, 2017

@Mamasquee It's a different bug then. See this one: #240

@jcheng5 jcheng5 closed this as completed Mar 23, 2017
@liosc
Copy link

liosc commented Apr 25, 2017

Thanks @kgavhane702

1 similar comment
@TimKettenacker
Copy link

Thanks @kgavhane702

@mjon
Copy link

mjon commented Feb 17, 2018

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

@kgavhane702
Copy link

Welcome

@eddiecylin
Copy link

@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

@kgavhane702
Copy link

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

@kgavhane702
Copy link

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

@eddiecylin
Copy link

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.

@jcheng5
Copy link
Member

jcheng5 commented Mar 29, 2018

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

@eddiecylin
Copy link

@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!

jpainter pushed a commit to jpainter/Surv_Assessment that referenced this issue Apr 2, 2018
1. map background not printing.  see rstudio/leaflet#192 for fix--adding url to addTiles()

2. fixed function for downloading all metadata
@kgavhane702
Copy link

Great..!

@jxu
Copy link

jxu commented Dec 12, 2018

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 

@rstudio rstudio locked as resolved and limited conversation to collaborators Dec 13, 2018
@schloerke
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests