18

I have a map leaflet that I want to save in an html file in a specific folder. I am using Windows 7.

I tried the following :

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)

As an error message, depending on the Rstudio session, I either have

1) Error in setwd(dir) : cannot change working directory

2) Cannot find path

When I only save like this :

library(htmlwidgets)
saveWidget(map_leaflet, file="test.html")

It works perfectly.

Thank you in advance for your help.

tuttifolies
  • 1,417
  • 2
  • 14
  • 20
  • it sounds like the directory `ressources` does not exist from the location where the code is being executed. Did you try checking `getwd()` in R just before running this code to make sure you're running it from the correct directory, and `dir()` to make sure that `ressources` exists at that location? If both of those look correct, a less favorable solution may be to use `setwd("ressources")` to change to that directory before trying to save. – user5359531 Dec 30 '16 at 19:09
  • 4
    Hi there, so I tried getwd() and dir() and ressources do appear correctly. I then tried this `path <- file.path(getwd(), "ressources", "test.html")` `saveWidget(map_leaflet, file=path)` and this time it works perfectly. I guess it just needed the complete path. Can you make your comment as an answer so that I can marked the question as answered? Thank you – tuttifolies Dec 31 '16 at 08:51
  • My tips were just troubleshooting. It still sounds like you're not executing the code from where you think you are. But as you found, using full paths, and using `file.path()` are more surefire ways to pass paths safely. You might also want to look at the `normalizePath()` function. It can help you get the absolute path, and can also let you know if the path doesn't exist or is incorrect. Try these and you will see: `normalizePath(".")`, `normalizePath("~")`, `normalizePath("./foo")` <- will give an error if `foo` does not exist. Also you may simply be unable to use relative paths for `file=` – user5359531 Jan 02 '17 at 01:38

2 Answers2

22

Agreed.

here is a workaround:

f<-"ressources\\test.html"
saveWidget(map_leaflet,file.path(normalizePath(dirname(f)),basename(f)))

The issues appear to be that saveWidget does not work with relative pathnames and normalizePath does not work for paths to files that done exist yet.

I would call this a bug in saveWidget.

edit:

I have contribute what I think is an even better workaround to an existing open issue

malcook
  • 1,686
  • 16
  • 16
0

I use the with_dir function in the withr package to do this. I also put it in a wrapper function:

save_leaflet <- function(plot, file, overwrite = FALSE){
  # save the file if it doesn't already exist or if overwrite == TRUE
  if( !file.exists(file) | overwrite ){
    withr::with_dir(new = dirname(file), 
                    code = htmlwidgets::saveWidget(plot, 
                                                   file = basename(file)))
  } else {
    print("File already exists and 'overwrite' == FALSE. Nothing saved to file.")
  }
}
filups21
  • 1,611
  • 1
  • 19
  • 22