3

I am trying to save some interactive figures into extra files. This works fine with htmlwidget::saveWidget. But I get problems by saving them into a different folder, for example into a results folder.

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'))

The error message is:

Error in normalizePath(basepath, "/", TRUE) :
path[1]="results": No such file or directory

Does somebody has an idea whats going on?

I am aware of just moving the file afterwards, but I would prefer to get this error message solved.

htmlwidgets::saveWidget(p, 'VSGs.html')
file.rename('VSGs.html', file.path(results_dir, 'VSGs.html'))
drmariod
  • 11,106
  • 16
  • 64
  • 110

3 Answers3

5

"results" doesn't seem to be a valid path
try setting a full path to folder which exists.
this should work:

dir.create(paste0(getwd(),"/results"))
results_dir = paste0(getwd(),"/results") # get directory

and then use results_dir as the path to save in.

Community
  • 1
  • 1
Dror Bogin
  • 453
  • 4
  • 13
  • 1
    Thanks, didn't thought about that, but with a global path it it working just fine. So I replaced the save command by `htmlwidgets::saveWidget(p, file.path(getwd(), results_dir, 'VSGs.html'))` – drmariod Feb 09 '18 at 06:33
  • I would reccomend setting a path you are sure in, and not just the woking directory. – Dror Bogin Feb 09 '18 at 06:34
  • 1
    To follow that comment, my results dir is relative to my working dir, thats why I think it is ok like this, only `saveWidget` isn't doing it correctly. One more comment on your code, it is preferred to use `file.path` over `paste0` since this is also taking care of windows or unix paths. Also you have a typo in `/rsults`... :-) didn't wanted to be a smart-ass :-) – drmariod Feb 09 '18 at 06:38
3

The underlying issue is discussed with a workaround in savewidget from htmlwidget in R , cannot save html file in another folder

TL/DR: use the following

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,...)
}
malcook
  • 1,686
  • 16
  • 16
  • I got your comment also an GitHub. Thanks. I now prefer to just create an absolute path with `file.path(getwd(), 'outdir', 'widget.html')` instead of creating an extra function. I don't want to change my code later one and remove my functions in case this ever get's fixed. – drmariod Mar 22 '18 at 06:53
0

you can also modify the HTMLs after rendering them using gnu-sed from homebrew... Make sure to use absolute path in the replacement.

sed -i 's/"old_library_directory/"common\/filepath\/new_library_directory/g' *.html

Here is a link to sed syntax --> https://ss64.com/osx/sed.html