【发布时间】:2017-05-14 23:09:22
【问题描述】:
我有一个地图传单,我想将它保存在特定文件夹中的 html 文件中。 我正在使用 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 会话,我有
1) setwd(dir) 中的错误:无法更改工作目录
2) 找不到路径
当我只这样保存时:
library(htmlwidgets)
saveWidget(map_leaflet, file="test.html")
效果很好。
提前感谢您的帮助。
【问题讨论】:
-
从执行代码的位置听起来好像目录
ressources不存在。您是否在运行此代码之前尝试在 R 中检查getwd()以确保您从正确的目录运行它,并检查dir()以确保ressources存在于该位置?如果这两个看起来都正确,一个不太有利的解决方案可能是在尝试保存之前使用setwd("ressources")更改到该目录。 -
您好,所以我尝试了 getwd() 和 dir() 并且 ressources 确实显示正确。然后我尝试了这个
path <- file.path(getwd(), "ressources", "test.html")saveWidget(map_leaflet, file=path),这一次它工作得很好。我想它只需要完整的路径。您能否发表评论作为答案,以便我可以将问题标记为已回答?谢谢 -
我的提示只是排除故障。听起来您仍然没有从您认为的位置执行代码。但正如您所发现的,使用完整路径和使用
file.path()是安全通过路径的更可靠方法。您可能还想查看normalizePath()函数。它可以帮助您获取绝对路径,也可以让您知道路径是否不存在或不正确。试试这些,你会看到:normalizePath("."),normalizePath("~"),normalizePath("./foo")foo 不存在,会报错。此外,您可能根本无法使用file=的相对路径
标签: html r save htmlwidgets