How to access map generated by leaflet in R

前端 未结 4 1847
情书的邮戳
情书的邮戳 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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...

提交回复
热议问题