R : combine and save rgl widgets as a single html file

混江龙づ霸主 提交于 2021-02-18 16:05:57

问题


In R, using the rgl and htmlwidgets libraries, I'm trying to extract an HTML file having a widget with toggle buttons.

The following example does what I want in the RStudio Viewer. The HTML export works without the toggle buttons, but fails when the rglwidget includes these buttons.

The first part is based on these rgl examples, and the export part on the htmlwidgets manual.

library(rgl)

open3d()

x <- sin(1:100)
y <- cos(1:100)
z <- -100:100/100

# draw a barrel
sids1 <- spheres3d(x, y, z, col = rainbow(1000),radius=0.05)
# draw a pole
sids2 <- spheres3d(0, 0, z, col = rainbow(1000),radius=0.05)

# create widgets with toggle buttons
widgets <- rglwidget() %>%
  toggleWidget(ids = sids1, label = "Toggle Barrel") %>%
  toggleWidget(ids = sids2, label = "Toggle Pole") 

# Works well in RStudio Viewer
if (interactive()) widgets

# HTML export works without the toggle buttons
htmlwidgets::saveWidget(rglwidget(), "x.html")

# HTML export fails with the toggle buttons
htmlwidgets::saveWidget(widgets, "y.html")

The second htmlwidgets::saveWidget line fails with

Error in system.file(config, package = package) : 
  'package' must be of length 1

Indeed, the widgets object is a list of 3 items (3 widgets!). Each item can be saved separately, with for example htmlwidgets::saveWidget(widgets[[1]], "y1.html") . This will produce 3 separate html files.

How to combine all these widgets together? According to this link, others have similar problems.

If this feature is not implemented, is there another package that would work?


回答1:


Your widgets object is a list of 3 widgets with class c("shiny.tag.list","list"), not a widget. You can save it using the htmltools::save_html function. So instead of

htmlwidgets::saveWidget(widgets, "y.html")

you want

htmltools::save_html(widgets, "y.html")


来源:https://stackoverflow.com/questions/47920720/r-combine-and-save-rgl-widgets-as-a-single-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!