R:处理 "yaml" 解析器错误

R: Dealing with "yaml" Parser Errors

我正在使用 R 编程语言。我正在尝试从此处的网站复制交互式“仪表板”:https://beta.rstudioconnect.com/jjallaire/htmlwidgets-rbokeh-iris/htmlwidgets-rbokeh-iris.html

首先,我运行此代码用于访问“flexdashboard 模板制作器”:

library(flexdashboard)
 
rmarkdown::draft("dashboard.Rmd", template = "flex_dashboard", package = "flexdashboard")

然后,我把弹出来的window里面的文字全部删掉了。我从网站 (https://beta.rstudioconnect.com/jjallaire/htmlwidgets-rbokeh-iris/htmlwidgets-rbokeh-iris.html) 将 R 代码(逐字逐句)复制到此 window 并单击“保存”:

---
title: "rbokeh iris dataset"
author: "Ryan Hafen"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(rbokeh)
library(flexdashboard)
```

Column {data-width=600}
-----------------------------------------------------------------------

### Species

```{r}
figure(width = NULL, height = NULL) %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species)
# figure() %>%
#   ly_points(Sepal.Length, Sepal.Width, data = iris,
#     color = Species, glyph = Species)
```


Column {data-width=400}
-----------------------------------------------------------------------

### Species (Quantile)

```{r}
figure(width = NULL, height = NULL, legend_location = "top_left") %>%
  ly_quantile(Sepal.Length, group = Species, data = iris)
```

### Petal Width

```{r}
figure(width = NULL, height = NULL) %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Petal.Width)
```

此文件("dashboard.Rmd")保存在"我的文档"中(也已设置为默认工作目录):

现在,我想“查看”仪表板并将仪表板“保存”为“.html”文件。按照另一个 Whosebug 问题 (R: Converting "rmarkdown" to "html" files) 的答案的建议,我在 R studio 中打开“RMD”文件:(“文件”->“打开文件”->“dashboard.rmd”)

然后,我点击了“编织”->“编织到flex_dashboard”:

但是,这会产生以下错误:

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Parser error: did not find expected <document start> at line 2, column 1
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted

有人可以告诉我我做错了什么以及如何解决这个错误吗?我正在尝试生成与原始网站 (https://beta.rstudioconnect.com/jjallaire/htmlwidgets-rbokeh-iris/htmlwidgets-rbokeh-iris.html) 相同的仪表板。这个 YAML 错误是什么?在第 2 行,第 1 列,究竟是什么没有被“解析”?代码开头的“标题”和“作者”信息是否有问题?是不是原代码有错误,还是我程序有问题?

谢谢

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252
[4] LC_NUMERIC=C                    LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmarkdown_2.7       knitr_1.31          flexdashboard_0.5.2

loaded via a namespace (and not attached):
 [1] compiler_4.0.3    htmltools_0.5.1.1 tools_4.0.3       yaml_2.2.1        tinytex_0.30      jsonlite_1.7.2   
 [7] digest_0.6.27     xfun_0.21         rlang_0.4.10      evaluate_0.14  

我想我能够找出问题所在。我将此代码复制并粘贴到 RMD 编辑器中,仪表板工作正常!

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(rbokeh)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
figure(width = NULL, height = NULL) %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species)
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart B

```{r}
figure(width = NULL, height = NULL, legend_location = "top_left") %>%
  ly_quantile(Sepal.Length, group = Species, data = iris)

```

### Chart C

```{r}
figure(width = NULL, height = NULL) %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Petal.Width)
```