简体   繁体   English

对于Markdown,Yaml标题中的单引号和双引号有什么区别?

[英]What is the difference between a single quote and double quote in Yaml header for r Markdown?

I am getting an error in my r Markdown file that I am compiling using knitr in RStudio. 我正在使用RStudio中的knitr编译我的Markdown文件中的错误。 I'm not really sure where this 'error' should be directed. 我不确定这个'错误'应该指向哪里。 It doesn't appear to be an 'R' error per say. 每个说法似乎不是'R'错误。

If I create an R markdown document with the following YAML header content, I can knit the file just fine: 如果我创建一个带有以下YAML标题内容的R markdown文档,我可以编辑该文件:

---
title: "Eye tracking AOI plots"
author: "Steven Vannoy"
date:  "`r format(Sys.time(), '%I:%M')`"
output: html_document
---

But if I merely change the single quotes inside the format statement to double quotes (which is what I was originally using), 但是,如果我只是将format语句中的单引号更改为双引号(这是我最初使用的),

---
title: "Eye tracking AOI plots"
author: "Steven Vannoy"
date:  "`r format(Sys.time(), "%I:%M")`"
output: html_document
---

I get the following run time error: 我得到以下运行时错误:

Error in yaml::yaml.load(enc2utf8(string), ...) : 
  Scanner error: while scanning for the next token at line 3, column 32found character that cannot start any token at line 3, column 32
Calls: <Anonymous> ... yaml_load_utf8 -> mark_utf8 -> <Anonymous> -> .Call
Execution halted

I experimented around enough to know that it is the colon ':' that is causing the problem, the error is not produced if you use "%A %d" for example. 我进行了足够的实验,知道它是冒号':'导致问题,如果您使用“%A%d”,则不会产生错误。

I searched around and found a number of assertions that single and double quotes are generally equivalent in R, although you can not pair a double quote with a single quote and have it act like two double quotes. 我在周围搜索并发现了一些断言,单引号和双引号在R中通常是等价的,尽管你不能将双引号与单引号配对并让它像两个双引号一样。

Obviously I have a working code sample that does what I need to do, but I generally use double quotes and am wondering how I can know when I should be using single quotes? 显然我有一个工作代码示例,它做了我需要做的事情,但我通常使用双引号,我想知道我怎么知道什么时候应该使用单引号?

That single and double quotes are generally equivalent in R (like they are eg in Python) is irrelevant, the parsing problem occurs on the YAML level. 单引号和双引号在R中通常是等价的(就像它们在Python中一样)是无关紧要的,解析问题发生在YAML级别上。

You don't need to quote scalars in YAML, but if you do, you need to know that double quoted style scalars ( " ) require escaping: 您不需要在YAML中引用标量,但如果您这样做,则需要知道双引号样式标量" )需要转义:

This is the only style capable of expressing arbitrary strings, by using “\\” escape sequences. 这是唯一能够通过使用“\\”转义序列表达任意字符串的样式。 This comes at the cost of having to escape the “\\” and “"” characters. 这是以逃避“\\”和“”字符为代价的。

So if you want to use double quotes within double quotes you have to do: 因此,如果您想在双引号内使用双引号,则必须执行以下操作:

---
title: "Eye tracking AOI plots"
author: "Steven Vannoy"
date:  "`r format(Sys.time(), \"%I:%M\")`"
output: html_document
---

That SabDeM's solution works as well is because there are no single quotes within the scalar SabDeM的解决方案也起作用,因为标量中没有单引号

`r format(Sys.time(), "%I:%M")`

single quoted style scalars however can only represent strings consisting only of printable characters. 但是,单引号样式标量只能表示仅包含可打印字符的字符串。


Scalars often don't need to be quoted in YAML at all, as you already do with the keys ( title , author , etc). Scalars通常根本不需要在YAML中引用,因为您已经使用了键( titleauthor等)。 But a plain style scalar cannot start with a backquote. 但是简单的样式标量不能以反引号开头。 I would have used the plain style for all scalars except the value for the date key and use literal style for that one (only), to get the IMO better readable: 我会使用除了date键的值之外的所有标量的普通样式 ,并使用该文字样式 (仅限),以使IMO更易读:

---
title: Eye tracking AOI plots
author: Steven Vannoy
date: |-
  `r format(Sys.time(), "%I:%M")`
output: html_document
---

Which is exactly equivalent to your YAML. 这与您的YAML完全相同。

As I said in my comment maybe the problem is that knitr does not know how to parse nested symbols or maybe the question might be related to the fact that to quote ` you need " and vice versa. This code works and gives a plus to the latter hypothesis: 正如我在评论中所说的那样,问题是knitr不知道如何解析嵌套符号,或者问题可能与引用“你需要"的事实有关,反之亦然。这段代码可以工作并给予加分后一种假设:

---
title: "Eye tracking AOI plots"
author: "Steven Vannoy"
date:  '`r format(Sys.time(), "%I:%M")`'
output: html_document
---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM