在 rmarkdown 中渲染 fusionchartsR htmlwidgets

Rendering fusionchartsR htmlwidgets in rmarkdown

我正在构建一个名为 fusionchartsR (https://github.com/alexym1) 的新 htmlwidget 包。我试图在我的 rmarkdown 报告中嵌入一小段代码,但是它不起作用,我也不知道为什么。我尝试了不同的策略但没有成功。

第一个攻略

---
title: "Stack overflow"
author: "John Doe"
date: "01/04/2020"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Render an htmlwidget graphic

```{r}
library(fusionchartsR)
df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
fusionPlot(data = df, type = 'pie2d') %>%
  fusionTheme(theme = "fusion") 
```

第二种策略

# Webshot and phantomjs have been previously installed.
library(webshot)
webshot::install_phantomjs()

# Then, I loaded packages and built a little piece of code
library(fusionchartsR)
library(htmlwidgets)

df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
widget <- fusionPlot(data = df, type = 'pie2d') %>%
  fusionTheme(theme = "fusion") 

# Save a rendered widget to an HTML file
saveWidget(widget = widget, file = "Mywidget.html")

# An error appeared: `Error: pandoc document conversion failed with error 99`

# Take a webshot
webshot(url = "Mywidget.html", file = "webshot.png")

可以在您的文档文件夹中找到 Mywidget.html。

我该如何解决这个问题? 我会非常感激!

您应该考虑在 https://github.com/rstudio/webshot2 which doesn't suffer from this issue. I have replicated your scenario with webshot2, the issue is resolved as below screenshot. See my detailed answer 上尝试 webshot2,而不是使用 webshot。

代码:

# Webshot and phantomjs have been previously installed.
library(webshot2)

# install.packages("remotes")
# remotes::install_github("alexym1/fusionChartsR")

# Then, I loaded packages and built a little piece of code
library(fusionchartsR)
library(htmlwidgets)

df <- data.frame(label = c("Venezuela", "Saudi", "Canada", "Russia"), value = c(290, 260,180, 115))
widget <- fusionPlot(data = df, type = 'pie2d') %>%
  fusionTheme(theme = "fusion") 

# Save a rendered widget to an HTML file
saveWidget(widget = widget, file = "Mywidget.html")

# An error appeared: `Error: pandoc document conversion failed with error 99`

# Take a webshot
webshot(url = "Mywidget.html", file = "webshot.png")

输出: