In RStudio I can save my html widget to a single file with:
htmlwidgets::saveWidget(x,"filename.html",selfcontained=TRUE)
When running an R script with the above line through windows Task Scheduler (which calls Rscript.exe) this would generate a multi-file html (i.e. an html file with an associated directory of referenced stuff). Using RGui also failed to generate a single-file html.
These are the lines from an Rgui implementation that replicates the issue:
> library(leaflet)
> library(htmlwidgets)
> map <- leaflet::leaflet() %>% addTiles() %>% setView(lng=172.4,lat=-41,zoom=6)
> saveWidget(map,file="deleteme.html",selfcontained=TRUE)
Error in saveWidget(map, file = "deleteme.html", selfcontained = TRUE) :
Saving a widget with selfcontained = TRUE requires pandoc. For details see:
https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md
> list.files(getwd(),full.names=FALSE, recursive=TRUE)
[1] "deleteme.html"
[2] "deleteme_files/htmlwidgets-1.5.3/htmlwidgets.js"
[3] "deleteme_files/jquery-1.12.4/jquery.min.js"
[4] "deleteme_files/leaflet-1.3.1/images/layers-2x.png"
[5] "deleteme_files/leaflet-1.3.1/images/layers.png"
[6] "deleteme_files/leaflet-1.3.1/images/marker-icon-2x.png"
[7] "deleteme_files/leaflet-1.3.1/images/marker-icon.png"
[8] "deleteme_files/leaflet-1.3.1/images/marker-shadow.png"
[9] "deleteme_files/leaflet-1.3.1/leaflet.css"
[10] "deleteme_files/leaflet-1.3.1/leaflet.js"
[11] "deleteme_files/leaflet-binding-2.0.4.1/leaflet.js"
[12] "deleteme_files/leafletfix-1.0.0/leafletfix.css"
[13] "deleteme_files/proj4-2.6.2/proj4.min.js"
[14] "deleteme_files/Proj4Leaflet-1.0.1/proj4leaflet.js"
[15] "deleteme_files/rstudio_leaflet-1.3.1/images/1px.png"
[16] "deleteme_files/rstudio_leaflet-1.3.1/rstudio_leaflet.css"
A clue to the cause is the error message returned after running the saveWidget function (though the web link provided no longer works). The error message points out that htmlwidgets::saveWidget function requires pandoc.exe application to be available for it to create a single-file html. If it is not available then it creates a multi-file html.
But I have pandoc.exe installed, as it comes with the RStudio bundle, which I use all the time.
As it turns out, the availability of pandoc.exe is checked by htmlwidgets::saveWidget using the rmarkdown::find_pandoc function. This checks for the existence of pandoc.exe in paths saved in the "RSTUDIO_PANDOC" and "PATH" environment variables and in the ~/opt/pandoc/ directory.
When using RStudio, the "RSTUDIO_PANDOC" environment variable is automagically set. It is not set when I run a script via Task Scheduler as, in that case I use Rscript.exe. When pandoc.exe is installed (it comes in the RStudio bundle) the PATH environment variable is not set so it can't be found that way either.
So how do I get saveWidget to save to a single html file when I'm not using RStudio?