来自 R 中 htmlwidget 的 savewidget,无法将 html 文件保存在另一个文件夹中

2022-01-24 00:00:00 r save html htmlwidgets

我有一个地图传单,我想将它保存在特定文件夹中的 html 文件中.我正在使用 Windows 7.

I have a map leaflet that I want to save in an html file in a specific folder. I am using Windows 7.

我尝试了以下方法:

library(htmlwidgets)
saveWidget(map_leaflet, file="ressources/test.html")

library(htmlwidgets)
saveWidget(map_leaflet, file="ressources\test.html")

library(htmlwidgets)
path_name <- file.path("ressources", "test.html", fsep="\")
saveWidget(map_leaflet, file=path_name)

library(htmlwidgets)
path_name <- paste("ressources", "test.html", sep="/")
saveWidget(map_leaflet, file=path_name)

作为错误消息,根据 Rstudio 会话,我有

As an error message, depending on the Rstudio session, I either have

1) setwd(dir) 中的错误:无法更改工作目录

1) Error in setwd(dir) : cannot change working directory

2) 找不到路径

当我只这样保存时:

library(htmlwidgets)
saveWidget(map_leaflet, file="test.html")

效果很好.

提前感谢您的帮助.

推荐答案

同意.

这里有一个解决方法:

f<-"ressources\test.html"
saveWidget(map_leaflet,file.path(normalizePath(dirname(f)),basename(f)))

问题似乎是 saveWidget 不适用于相对路径名,而 normalizePath 不适用于已存在的文件的路径.

The issues appear to be that saveWidget does not work with relative pathnames and normalizePath does not work for paths to files that done exist yet.

我认为这是 saveWidget 中的一个错误.

I would call this a bug in saveWidget.

我为一个现有的未解决问题贡献了我认为更好的解决方法

I have contribute what I think is an even better workaround to an existing open issue

相关文章