Open mac471 opened 6 years ago
I'm also having this issue, currently removing the additional generated files manually R 3.3.0 htmlwidgets 0.9
Yes, saveWidget
seems to expect the working directory to be the same directory as the output html file, but both pandoc_save_markdown
and pandoc_self_contained_html
(or pandoc_convert
) set the working directory back to the original state on.exit
.
This could potentially be bad news if you happened to have a directory named libdir in your original working directory. For now, it might be prudent to pass the full path to the libdir instead of using default NULL.
note that if you pass an incorrect or incomplete path to libdir, then it will delete all files in that path.
As mentioned in my comment https://github.com/ramnathv/htmlwidgets/issues/299#issuecomment-869012044, this is still the case. Here's a reprex illustrating the issue with the latest CRAN version of htmlwidgets
(1.5.3):
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
library(htmlwidgets)
p <- plot_ly(
mtcars,
x = ~wt,
y = ~mpg,
type = "scatter",
mode = "markers"
)
# This works as expected
saveWidget(
p,
"mtcars.html", # in the current working directory
selfcontained = TRUE # which is the default anyway
)
dir.exists("mtcars-files")
#> [1] FALSE
# When saving to a relative path, an unnecessary temporary directory remains
dir.create("my-dir")
saveWidget(
p,
file.path("my-dir", "mtcars.html"), # in the directory just created
selfcontained = TRUE # which is the default anyway
)
dir.exists(file.path("my-dir", "mtcars_files"))
#> [1] TRUE
Created on 2021-07-15 by the reprex package (v2.0.0)
I used a temporary solution: save it in working directory and move it afterwards.
htmlwidgets::saveWidget(widget = as_widget(p), file = "tmp.html", selfcontained = TRUE) file.rename("tmp.html", "target_dir/target.html"))
Same for me, and also affecting packages that use this under the hood (e.g., in my case, reactablefmtr::save_reactable()).
In the meantime, you can use the following workaround:
saveWidget2 = function(widget, file, ...){
wd = setwd(dirname(file))
on.exit(setwd(wd))
htmlwidgets::saveWidget(widget, file=basename(file), ...)
}
When
selfcontained=TRUE
in saveWidget, the folder created with all of the dependent files is not removed when the path of thefile
is not the same as the current working directory.In the code fragment below, note that
libdir
is the base name offile
plus_files
appended. The path has been removed. If the current working directory is not the same as the path tofile
, this commandunlink(libdir, recursive = TRUE)
fails.To make this more robust, suggest the following change to the
unlink
function call:Thanks. Keep up the great work!