rplotlyhtmlwidgets

saving interactive plotly graph to a path using htmlwidget


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

Solution

  • "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.