首页 > 解决方案 > 在 rmarkdown 中渲染 fusionchartsR htmlwidgets

问题描述

我正在构建一个名为fusionchartsRhttps://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 可以在您的 Documents 文件夹中找到。

我怎么解决这个问题 ?我会很感激的!

标签: rr-markdownfusionchartshtmlwidgets

解决方案


而不是使用 webshot,您应该考虑在https://github.com/rstudio/webshot2上尝试webshot2 ,它不会受到此问题的影响。我已经使用 webshot2 复制了您的场景,问题已解决,如下图所示。请参阅我对类似案例的详细回答。

编码:

# 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")

输出: 在此处输入图像描述


推荐阅读