使用htmlwidget将交互式plotly图形保存到路径

时间:2018-02-08 16:56:15

标签: r plotly htmlwidgets

我正在尝试将一些交互式数据保存到额外的文件中。这适用于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'))

2 个答案:

答案 0 :(得分:2)

&#34;结果&#34;似乎不是一个有效的路径 尝试设置存在的文件夹的完整路径 这应该有效:

dir.create(paste0(getwd(),"/results"))
results_dir = paste0(getwd(),"/rsults")

然后使用results_dir作为保存的路径。

答案 1 :(得分:1)

使用savewidget from htmlwidget in R , cannot save html file in another folder

中的解决方法讨论了基本问题

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,...)
}