我正在尝试将一些交互式图形保存到额外的文件中。这与
htmlwidget::saveWidget
配合得很好。但我将它们保存到不同的文件夹中时遇到问题,例如保存到结果文件夹中。
results_dir <- 'results'
if(!dir.exists(results_dir)) dir.create(results_dir)
p <- plotly::plot_ly(economics, x = ~date, y = ~pop,
type = 'scatter', mode = 'markers')
htmlwidgets::saveWidget(p,
file.path(results_dir, 'VSGs.html'))
错误信息是:
normalizePath(basepath, "/", TRUE) 中的错误:
path[1]="results": 没有这样的文件或目录
有人知道发生了什么事吗?
我知道之后只是移动文件,但我更希望解决此错误消息。
htmlwidgets::saveWidget(p, 'VSGs.html')
file.rename('VSGs.html', file.path(results_dir, 'VSGs.html'))
“结果”似乎不是有效的路径
尝试设置存在的文件夹的完整路径。
这应该有效:
dir.create(paste0(getwd(),"/results"))
results_dir = paste0(getwd(),"/results") # get directory
然后使用results_dir作为保存路径。
通过 savewidget from htmlwidget in R 中的解决方法讨论了根本问题,无法将 html 文件保存在另一个文件夹中
TL/DR:使用以下内容
saveWidgetFix <- function (widget,file,...) {
## A wrapper to saveWidget which compensates for arguable BUG in
## saveWidget which requires `file` to be in current working
## directory.
wd<-getwd()
on.exit(setwd(wd))
outDir<-dirname(file)
file<-basename(file)
setwd(outDir);
saveWidget(widget,file=file,...)
}
您还可以在使用自制程序中的 gnu-sed 渲染 HTML 后修改它们...确保在替换中使用绝对路径。
sed -i 's/"old_library_directory/"common\/filepath\/new_library_directory/g' *.html
这里是 sed 语法的链接 --> https://ss64.com/osx/sed.html