R read_yaml() reads a vector as parameter

那年仲夏 提交于 2021-02-10 06:29:08

问题


I would like to read a .yaml file to get yaml parameters for a Rmarkdown report. Original I have a yaml header to define a vector.

---
params:
  ids: !r c(2455, 2490) 
---

and it works, where params$ids is a vector.

However, if I put ids: !r c(2455, 2490) into a report_params.yaml file, and read that yaml file by

report_params <- yaml::read_yaml("report_params.yaml")

now report_params$ids is a string 'c(2455, 2490)'. so what did I miss, and how should I fix this?


回答1:


The YAML default handler uses !expr rather than !r.

report_params.yaml:

---
params:
  ids: !expr c(2455, 2490)
---
yaml::read_yaml("report_params.yaml")
#> $params
#> $params$ids
#> [1] 2455 2490


来源:https://stackoverflow.com/questions/64454623/r-read-yaml-reads-a-vector-as-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!