How to access map generated by leaflet in R

前端 未结 4 1833
情书的邮戳
情书的邮戳 2020-12-31 14:28

Let\'s say I have a code like this

# Install devtools if needed
if(!require(devtools)) install.packages(\"devtools\")
# view rawif-devtools.R hosted with ❤ b         


        
相关标签:
4条回答
  • 2020-12-31 14:58

    When I try to install a package named "leaflet", the dialog with CRAN only shows a package named leafletR. Installing and loading that package does succeed with a message to the console:

     Your leaflet map has been saved under /Users/myuser_name/map/map.html
    

    And that map has the desired functionality. Given the amount of information that I can access from the web browser, I'm guessing that I'm actually interfaced through Chrome to an OpenStreetMap server rather than interacting with a disk file data service.

    There is no addTiles function in the version downloaded from CRAN. And using sos::findFn does not find it in any other package. This could be a new function only available in the github version: https://github.com/chgrl/leafletR

    Further searching shows this to be only hosted on RStudio and not on CRAN: http://robinlovelace.net/r/2015/02/01/leaflet-r-package.html

    I needed a fresh session since I was getting error that I suspected were cause by having both leaflet and leafletR loaded at the same time. In my browser I left-clicked to bring up a ViewSource window and then selected and copy below. Both Chrome and Firefox have the capacity to display the underlying code and support selection and copying to an editor.

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <script src="lib/htmlwidgets-0.3.2/htmlwidgets.js"></script>
    <script src="lib/jquery-1.11.1/jquery.min.js"></script>
    <link href="lib/leaflet-0.7.3/leaflet.css" rel="stylesheet" />
    <script src="lib/leaflet-0.7.3/leaflet.js"></script>
    <link href="lib/leafletfix-1.0.0/leafletfix.css" rel="stylesheet" />
    <script src="lib/leaflet-binding-0.0.16/leaflet.js"></script>
    
    </head>
    <body style="background-color:white;">
    <div id="htmlwidget_container">
      <div id="htmlwidget-3689" style="width:100%;height:400px;" class="leaflet"></div>
    </div>
    <script type="application/json" data-for="htmlwidget-3689">{ "x": {
     "calls": [
     {
     "method": "addTiles",
    "args": [
     "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
    null,
    {
     "minZoom":                 0,
    "maxZoom":                18,
    "maxNativeZoom": null,
    "tileSize":               256,
    "subdomains": "abc",
    "errorTileUrl": "",
    "tms": false,
    "continuousWorld": false,
    "noWrap": false,
    "zoomOffset":                 0,
    "zoomReverse": false,
    "opacity":                 1,
    "zIndex": null,
    "unloadInvisibleTiles": null,
    "updateWhenIdle": null,
    "detectRetina": false,
    "reuseTiles": false,
    "attribution": "&copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>" 
    } 
    ] 
    } 
    ] 
    },"evals": [  ] }</script>
    <script type="application/htmlwidget-sizing" data-for="htmlwidget-3689">{ "viewer": {
     "width": "100%",
    "height":               400,
    "padding":                 0,
    "fill": true 
    },"browser": {
     "width": "100%",
    "height":               400,
    "padding":                 0,
    "fill": true 
    } }</script>
    </body>
    </html>
    

    The code alone is not enough. The rest of the required support files will be saved in a directory with the same name as the html file and the browser "Save As ..." function is best for that:

    enter image description here

    0 讨论(0)
  • 2020-12-31 15:04

    Once you opened your map in chrome (or any other browser) you can simply go to "File -> Save as" and then save the .html page to a target directory under a user defined name. (Or just press cmd + s on mac or ctrl + s on windows.) This is usually what I do when I create a webmap with R or Rmarkdown.


    Of course you can also upload your file to Rpubs.com by clicking on the "Publish" button, when you create your map with Rmarkdown. From there you can easily access it via a provided link.

    0 讨论(0)
  • 2020-12-31 15:13

    I developed a couple of functions that lets you save a leaflet map somewhere other than a temp folder.

    See the gist here: https://gist.github.com/barryrowlingson/d066a7ace15cf119681a for the full info, the short version is these two functions:

    saveas <- function(map, file){
        class(map) <- c("saveas",class(map))
        attr(map,"filesave")=file
        map
    }
    
    print.saveas <- function(x, ...){
        class(x) = class(x)[class(x)!="saveas"]
        htmltools::save_html(x, file=attr(x,"filesave"))
    }
    

    then all you do is:

    leaflet() %>% etc etc %>% saveas("/wherever/you/want/index.html")
    

    or in your mode of working:

    mymap <- leaflet()
    mymap <- addwhatever(mymap)
    saveas(mymap, "/wherever/you/want/index.html")
    

    At that point the folder /wherever/you/want should have a self-contained set of files for the map. I think it should be portable, ie work on any web server, but I can't guarantee that...

    0 讨论(0)
  • 2020-12-31 15:18

    a late response:

    library(leaflet)
    mymap <- leaflet() %>%
      addTiles()
    library(htmlwidgets)
    saveWidget(widget = mymap, file = "/wherever/you/want/mymap.html")
    

    einar

    0 讨论(0)
提交回复
热议问题