22

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
svannoy
  • 1,935
  • 2
  • 14
  • 14
  • I think that it might be related to the fact that in R to quote `\`` (backticks) you have to use standard quote `"`, and vice versa so to quote standard quote you have to use backticks. It might be. – SabDeM Jul 13 '15 at 22:40
  • Or because some elements interfere with YAML from matter and the are reserved only to parsing YAML format. – SabDeM Jul 13 '15 at 22:48
  • After a search I've posted my solution to [this](http://stackoverflow.com/questions/23449319/yaml-current-date-in-rmarkdown/31395020#31395020) other question. At this point this one could be marked as a duplicate. – SabDeM Jul 13 '15 at 23:13
  • 1
    You can't have double quotes inside doubles quotes... you end up with two strings. Your choices are to escape the interior quotes with a backslash or use a different type of quote (single quote). (And vice versa switching double and single quotes.) The backticks have nothing to do with it. – Gregor Thomas Jul 13 '15 at 23:22
  • @Gregor I see that now, the YAML parser was "closing" my string at the second double quote (just before %I). By using the single quote inside the format statement. it did not pair that with the external double quote mark. Hard to describe in words, but I think it makes sense. My original use of nested double quotes essentially exposed the offensive colon and caused the error. – svannoy Jul 14 '15 at 21:54
  • @Gregor The backticks have in so far something to do with this is that their presence forces you to use quoting around the string scalar in flow style, and that leads to the OPs quote confusion. – Anthon Jul 15 '15 at 05:09

2 Answers2

16

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.

Anthon
  • 69,918
  • 32
  • 186
  • 246
1

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
---
SabDeM
  • 7,050
  • 2
  • 25
  • 38