Savewidget From Htmlwidget In R , Cannot Save Html File In Another Folder
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='ress
Solution 1:
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
Solution 2:
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 == TRUEif( !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.")
}
}
Post a Comment for "Savewidget From Htmlwidget In R , Cannot Save Html File In Another Folder"