htmlwidgets::saveWidget()中selfcontained参数弃用,如何使用新参数?
问题
用htmlwidgets::saveWidget(..., selfcontained = TRUE)保存plotly交互式图表为独立HTML文件时,一直弹出警告:
[WARNING] Deprecated: --self-contained. use --embed-resources --standalone
不知道怎么在这个函数里用embed-resources或standalone当参数,已经更新了RStudio和相关包,警告还是没消失。
使用环境:
- R 4.2.1
- RStudio 2022.07.2+576 "Spotted Wakerobin"
htmlwidgets1.5.4
用到的代码:
library(plotly) library(htmlwidgets) fig <- plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines") htmlwidgets::saveWidget(partial_bundle(fig), file = "plotly.html", selfcontained = TRUE) utils::browseURL("plotly.html")
解决方案
这个警告是因为你使用的pandoc版本更新了,它废弃了--self-contained参数,改用--embed-resources和--standalone,但当前版本的htmlwidgets还没适配这个变化,给你两个处理办法:
办法一:直接忽略警告
虽然弹出警告,但selfcontained = TRUE的功能还是正常的,生成的HTML依然是独立文件,不影响使用。
办法二:手动传递pandoc新参数
把selfcontained = TRUE删掉,改用extra_args参数传入pandoc的新参数,同时设置libdir = NULL避免生成外部资源文件夹,修改后的代码如下:
library(plotly) library(htmlwidgets) fig <- plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines") htmlwidgets::saveWidget( partial_bundle(fig), file = "plotly.html", libdir = NULL, extra_args = c("--embed-resources", "--standalone") ) utils::browseURL("plotly.html")
等后续htmlwidgets更新到适配新版pandoc的版本,应该会直接支持新参数,到时就不用手动传extra_args了。
内容的提问来源于stack exchange,提问作者bt3
相关产品推荐
相关产品推荐

