Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

yaml

knitr

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.

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

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

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.

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?

like image 229
svannoy Avatar asked Jul 13 '15 22:07

svannoy


People also ask

What is the difference between a single and double quote?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

Do quotes matter in YAML?

For most scalars you don't need any quotes at all, but if you need to define some piece of data which contains characters that could be mistaken with YAML syntax you need to quote it in either double " or single ' quotes for the YAML file to stay valid.

What's the difference between a single quote and a double quote in bash?

Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain \ escapes, etc. Enclosing characters in single quotes ( ' ) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.


2 Answers

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.

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

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.


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

Which is exactly equivalent to your YAML.

like image 112
Anthon Avatar answered Oct 17 '22 22:10

Anthon


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:

---
title: "Eye tracking AOI plots"
author: "Steven Vannoy"
date:  '`r format(Sys.time(), "%I:%M")`'
output: html_document
---
like image 1
SabDeM Avatar answered Oct 17 '22 22:10

SabDeM