在 R Markdown 中嵌入 networkD3,为什么会出现这个错误?

Embedding networkD3 in R Markdown, why this error?

我正在尝试将网络图添加到 R Markdown 文件中并写一篇论文。我能够使用 NetworkD3 将图形可视化,使图形变得很好。我想创建一个交互式 HTML,但我不断收到错误。这是一个例子:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
src <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
simpleNetwork(networkData)
```

然后我收到此错误,这与我在尝试嵌入自己的图表时收到的错误非常相似。

processing file: w.Rmd
  |........                                                         |  12%
  ordinary text without R code

  |................                                                 |  25%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |........................                                         |  38%
label: unnamed-chunk-1
Quitting from lines 12-16 (w.Rmd) 
Error in eval(expr, envir, enclos) : 
  could not find function "simpleNetwork"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval 
-> eval

Execution halted

您需要在您的设置中加载 networkD3 包,就像这样...

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(networkD3)
```
```{r}
src <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
simpleNetwork(networkData)
```

我发现我做错了什么。我必须将我的数据加载到 Markdown 文件中。我在这里 https://support.rstudio.com/hc/en-us/community/posts/200631198-data-set-not-found-when-knitting-HTML 找到了答案。