r Markdown的Yaml标头中的单引号和双引号有什么区别? [英] What is the difference between a single quote and double quote in Yaml header for r Markdown?

查看:100
本文介绍了r Markdown的Yaml标头中的单引号和双引号有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R Studio中使用knitr编译的r Markdown文件中出现错误.我不确定该错误"应指向何处.每句话似乎都不是'R'错误.

I am getting an error in my r Markdown file that I am compiling using knitr in RStudio. I'm not really sure where this 'error' should be directed. It doesn't appear to be an 'R' error per say.

如果我使用以下YAML标头内容创建R降价文档,则可以很好地编织文件:

If I create an R markdown document with the following YAML header content, I can knit the file just fine:

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

但是如果我只是将format语句中的单引号更改为双引号(这是我最初使用的格式),

But if I merely change the single quotes inside the format statement to double quotes (which is what I was originally using),

---
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

我进行了足够的实验,以了解引起问题的原因是冒号':',例如,如果使用%A%d",则不会产生错误.

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.

我四处搜寻,发现许多断言表明R中的单引号和双引号通常是等效的,尽管您不能将双引号与单引号配对,并且使它像两个双引号一样.

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.

很显然,我有一个工作代码示例可以完成我需要做的事情,但是我通常使用双引号,并且想知道如何知道何时应该使用单引号?

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?

推荐答案

R中的单引号和双引号通常是等效的(例如在Python中是不相关的),解析问题发生在YAML级别.

That single and double quotes are generally equivalent in R (like they are e.g. in Python) is irrelevant, the parsing problem occurs on the YAML level.

您不需要在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:

这是唯一能够通过使用"\"转义序列来表达任意字符串的样式.这是以必须转义"\"和""字符为代价的.

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
---


SabDeM的解决方案之所以有效,是因为标量内没有单引号


That SabDeM's solution works as well is because there are no single quotes within the scalar

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

单引号样式标量只能表示仅由可打印字符组成的字符串.

single quoted style scalars however can only represent strings consisting only of printable characters.

标量通常根本不需要用YAML引用,就像您已经使用过的键( title author 等)一样.但是普通样式的标量不能以反引号开头.我本该对所有标量使用纯样式,但的值除外>日期键,然后对该键使用文字风格,以使IMO更具可读性:

Scalars often don't need to be quoted in YAML at all, as you already do with the keys ( title, author, etc). 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:

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

这完全等同于您的YAML.

Which is exactly equivalent to your YAML.

这篇关于r Markdown的Yaml标头中的单引号和双引号有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆