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?
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.
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.
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.
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.
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
---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With