rmarkdown::render 不会渲染 rgl::plot3d 图形(但 Knit 按钮可以!)
请考虑以下示例:
---
title: "Untitled"
output:
pdf_document: default
---
```{r, setup}
Sys.setenv(CHROMOTE_CHROME = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe")
options(rgl.useNULL = TRUE)
rgl::setupKnitr()
```
Plot:
```{r, rgl = TRUE, dev = "png"}
rgl::plot3d(rnorm(10), rnorm(10), rnorm(10))
```
运行 rmarkdown::render("temp.Rmd", output_format = "pdf_document")
会生成没有图形的 PDF。更有趣的是,点击 RStudio 中的 Knit 按钮会生成完美的 PDF! (这本身很奇怪;我的印象是 Knit 按钮与上面的调用完全相同。)
Consider the following example:
---
title: "Untitled"
output:
pdf_document: default
---
```{r, setup}
Sys.setenv(CHROMOTE_CHROME = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe")
options(rgl.useNULL = TRUE)
rgl::setupKnitr()
```
Plot:
```{r, rgl = TRUE, dev = "png"}
rgl::plot3d(rnorm(10), rnorm(10), rnorm(10))
```
Running rmarkdown::render("temp.Rmd", output_format = "pdf_document")
produces a PDF with no figure. Even more interestingly, hitting the Knit button in RStudio produces a perfect PDF! (Which is quite strange itself; I was under the impression that the Knit button is exactly the same as the above call.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
rgl = TRUE
与hook_rgl
函数配合使用,这是在knitr
中处理rgl
的旧方法之一。它总是存在问题,因此最近的版本推荐了其他方法。使用 rgl::setupKnit(autoprint = TRUE) 是最简单的一种。它近似于基本绘图的行为:不会出现对绘图的细微更新,仅显示最终版本。如果您不希望这样,则可以将其保留为
autoprint = FALSE
,并显式包含rglwidget()
调用以显示绘图。自动打印并不完美,例如,一些生成 rgl 图的包不会以触发它的方式生成输出。您可以在
?rgl::setupKnit
帮助主题中阅读有关此内容的更多信息。Using
rgl = TRUE
works with thehook_rgl
function, which is one of the older ways of handlingrgl
inknitr
. It was always problematic, so recent releases have recommended other approaches instead.Using
rgl::setupKnitr(autoprint = TRUE)
is the easiest one. It approximates the behaviour of base plotting: minor updates to a plot won't appear, only the final version is shown. If you don't want that, then you can leave it withautoprint = FALSE
, and explicitly includerglwidget()
calls to get a plot to appear.The auto-printing isn't perfect, e.g. some packages that produce
rgl
plots don't produce output in a way that will trigger it. You can read more about this in the?rgl::setupKnitr
help topic.