【问题标题】:Why are some strings in quotes but others aren't when creating a .YAML file from R?为什么在从 R 创建 .YAML 文件时,一些字符串在引号中,而另一些则不是?
【发布时间】:2021-05-18 06:49:06
【问题描述】:

我正在尝试创建以下.YAML 文件:

summary:
  title: "Table tabs"
  link: ~
  blocks: []
  nested: nav-pills
  nested_names: yes

(请注意,波浪号、方括号或yes 周围没有引号)。

我编写代码在 R 中创建它:

tabs <- list(
        summary = 
            list(
                title = "Table tabs", 
                link = "~", 
                blocks = "[]", 
                nested = "nav-pills",
                nested_names = "yes"
                )
            )

write(yaml::as.yaml(tabs), file = "myfile.yaml"

但是当我将它写到 .YAML 时,它看起来像这样:

summary:
  title: Table tabs
  link: '~'
  blocks: '[]'
  nested: nav-pills
  nested_names: 'yes'

即波浪号、方括号和是的周围有引号。

为什么会发生这种情况,我可以做些什么来防止它?

【问题讨论】:

    标签: r yaml


    【解决方案1】:

    stackoverflow 中已经提供了这些信息: 我试图通过给定的答案向您指出:

    在问题“YAML: Do I need quotes for strings in YAML?”中充分讨论了在 yaml 中使用引号的更一般注意事项

    这里讨论'"在yaml中的区别: "What is the difference between a single quote and double quote in Yaml header for r Markdown?"

    这里专门讨论波浪号: "What is the purpose of tilde character ~ in YAML?"

    总结一下,

    波浪号是可以写入空值的一种方式。最多 解析器也接受 null 的空值,当然还有 null,Null 和空

    【讨论】:

      【解决方案2】:

      根据TarJae的回答,解决方法如下:

      tabs <- list(
              summary = 
                  list(
                      title = "Table tabs", 
                      link = NULL, 
                      blocks = list(), 
                      nested = "nav-pills",
                      nested_names = TRUE
                      )
                  )
      

      【讨论】:

        猜你喜欢
        • 2022-09-28
        • 1970-01-01
        • 2013-12-03
        • 2013-06-14
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 2021-03-30
        • 2020-01-12
        相关资源
        最近更新 更多