将传单输出保存为 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30110377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving leaflet output as html
提问by h.l.m
I am using RStudio to create some some leaflet images.
我正在使用 RStudio 创建一些传单图像。
I would like to be able to save the output as an HTML so that it can be emailed and others can view it.
我希望能够将输出保存为 HTML,以便可以通过电子邮件发送并且其他人可以查看它。
Below is some sample R code which was taken from [here] to create a sample leaflet image.
下面是一些从 [here] 中获取的示例 R 代码,用于创建示例传单图像。
devtools::install_github('rstudio/leaflet')
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m
Any code to be able to the output as HTML would be much appreciated...
任何能够输出为 HTML 的代码都将不胜感激......
回答by einar
Something like:
就像是:
library(htmlwidgets)
saveWidget(m, file="m.html")
seems to work on most widgets.
似乎适用于大多数小部件。
回答by maRtin
Open a new RMarkdown document. When you are using RStudio go to File -> New File -> R Markdown
.
Once you saved the file, you can insert your code into a chunk, like this:
打开一个新的 RMarkdown 文档。当您使用 RStudio 时,请转到File -> New File -> R Markdown
. 保存文件后,您可以将代码插入到一个块中,如下所示:
---
title: "Leaflet Map"
output: html_document
---
```{r}
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m
```
Then Press the Knit HTML
Button above the code window and your application will open in a new HTML file. You can send the file via eMail or upload it to your ftp.
然后按下Knit HTML
代码窗口上方的按钮,您的应用程序将在一个新的 HTML 文件中打开。您可以通过电子邮件发送文件或将其上传到您的 ftp。
回答by RgrNormand
I have faced the same problem and after installing Github version the problem was fixed.
我遇到了同样的问题,安装 Github 版本后问题得到解决。
# Or Github version
if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')
My present version is 1.1.0.9000, running on macOS Sierra, RStudio Version 1.1.232 and R 3.4.0
我目前的版本是 1.1.0.9000,在 macOS Sierra、RStudio 版本 1.1.232 和 R 3.4.0 上运行
You can export from RStudio or save using htmlwidgets.
您可以从 RStudio 导出或使用 htmlwidgets 保存。
回答by trevi
Another option using mapview
library is:
使用mapview
库的另一个选择是:
library(mapview)
mapshot(m, url = "m.html")
library(mapview)
mapshot(m, url = "m.html")
Note that you can also set the output to .png
, .pdf
, or .jpeg
.
请注意,您还可以将输出设置为.png
,.pdf
或.jpeg
。
回答by Pablo Naret
library(mapview)
图书馆(地图视图)
To save as a "png" or "jpg" image:
保存为“png”或“jpg”图像:
mapshot(m, file = "m.png")
mapshot(m, file = "m.jpeg")
Even pdf can be used
连pdf都可以用
回答by Carlos Beltrán
Both solutions saveWidget or mapshot work correctly (saveWidget seems to be quicker), however, you should take care with color selection, especially in those choosed for borders/lines of polygons because in the stored map not all colours in borders are drawn ("grey50" for example is ignored while pure colours as "black" are drawn normally).
两种解决方案 saveWidget 或 mapshot 都可以正常工作(saveWidget 似乎更快),但是,您应该注意颜色选择,尤其是在为边界/多边形线选择的颜色中,因为在存储的地图中,并非所有边界颜色都被绘制(“grey50 ” 例如被忽略,而纯颜色为“黑色”被正常绘制)。
Curiously, these colours are stored and shown correctly when they are used as fill colour.
奇怪的是,当这些颜色用作填充颜色时,它们会被正确存储和显示。