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


i getting error in r markdown file compiling using knitr in rstudio. i'm not sure 'error' should directed. doesn't appear 'r' error per say.

if create r markdown document following yaml header content, can knit file fine:

--- title: "eye tracking aoi plots" author: "steven vannoy" date:  "`r format(sys.time(), '%i:%m')`" output: html_document --- 

but if merely change single quotes inside format statement double quotes (which using),

--- title: "eye tracking aoi plots" author: "steven vannoy" date:  "`r format(sys.time(), "%i:%m")`" output: html_document --- 

i following run time error:

error in yaml::yaml.load(enc2utf8(string), ...) :    scanner error: while scanning next token @ line 3, column 32found character cannot start token @ line 3, column 32 calls: <anonymous> ... yaml_load_utf8 -> mark_utf8 -> <anonymous> -> .call execution halted 

i experimented around enough know colon ':' causing problem, error not produced if use "%a %d" example.

i searched around , found number of assertions single , double quotes equivalent in r, although can not pair double quote single quote , have act 2 double quotes.

obviously have working code sample need do, use double quotes , wondering how can know when should using single quotes?

that single , double quotes equivalent in r (like e.g. in python) irrelevant, parsing problem occurs on yaml level.

you don't need quote scalars in yaml, if do, need know double quoted style scalars (") require escaping:

this style capable of expressing arbitrary strings, using “\” escape sequences. comes @ cost of having escape “\” , “"” characters.

so if want use double quotes within double quotes have do:

--- title: "eye tracking aoi plots" author: "steven vannoy" date:  "`r format(sys.time(), \"%i:%m\")`" output: html_document --- 

that sabdem's solution works because there no single quotes within scalar

`r format(sys.time(), "%i:%m")` 

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


scalars don't need quoted in yaml @ all, keys ( title, author, etc). plain style scalar cannot start backquote. have used plain style scalars except value date key , use literal style 1 (only), imo better readable:

--- title: eye tracking aoi plots author: steven vannoy date: |-   `r format(sys.time(), "%i:%m")` output: html_document --- 

which equivalent yaml.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -