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

I get an error in the r Markdown file, which I compile with knitr in RStudio. I am not sure where this error should be directed. This does not seem to be an "R" error.

If I create a document with R markup with the following contents of the YAML header, I can simply insert the file:

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

But if I just change the single quotes inside the format operator to double quotes (this is what I originally used),

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

I get the following runtime 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 have experimented enough to know that it is the colon ':' that causes the problem, the error does not occur if you use, for example, "% A% d".

I searched and found a number of claims that single and double quotes are usually equivalent in R, although you cannot match a double quote with a single quote and make it act like two double quotes.

Obviously, I have a sample working code that does what I need to do, but I usually use double quotes and wonder, how can I know when I should use single quotes?

+6
source share
2 answers

These single and double quotes, as a rule, are equivalent in R (as, for example, in Python), they do not matter, the problem of parsing arises at the YAML level.

You do not need to specify scalars in YAML, but if you do, you need to know that double style quotes ( " ) require escaping:

This is the only style that can express arbitrary strings using "\" escape sequences. This is due to the fact that you need to avoid the characters "\" and "".

So, if you want to use double quotes in double quotes, you should do:

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

This SabDeM solution also works because there are no quotes in the scalar

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

single style quotes, however, can only represent strings consisting only of printable characters.


Scalars often do not need to be specified in YAML at all, as you already did with the keys ( title , author , etc.). But a simple style scalar cannot start with backquote. I would use the regular style for all scalars, except for the value for the date key, and use the literal style for this (only) to get the IMO more readable:

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

This exactly matches your YAML.

+8
source

As I said in my comment, the problem is that knitr does not know how to parse nested characters, or maybe the question may be related to what you need to quote and vice versa. This code works and gives plus to the last hypothesis:

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

All Articles