13

I'm trying to create some snippet templates for knitr::spin documents in R Studio, and I need them to include literal backticks so that the resulting document contains an R snippet:

Example of desired output:

#' ---
#' author: 'ENTER'
#' title: 'ENTER'
#' date: '`r Sys.time()`'
#' output: 
#'    html_document
#' ---

However, I can't figure out how to output backticks. This:

`r paste("#' date: '`Sys.time()`')`

will not work as the tick marks interrupt the paste command when rendering from R Studio snippet to R code. I've been trying to hash out the tick marks, adding forwards and backwards slashes etc., but haven't found a solution that renders this line correctly to:

#' date: '`r Sys.time()`'

Windows 7 Enterprise, 64-bit Operating System
R Version: 3.2.5
R Studio Version: 0.99.903
knitr Version: 1.14


Example: I tried this, but it still translates the R code instead of just printing the text:

snippet spin.header
`r paste("#' ---")`
`r paste("#' author: 'ENTER'")`
`r paste("#' title: 'ENTER'")`
`r paste("#' date:  '<code>``` `r Sys.time()` ```</code>'")`
`r paste("#' output:")` 
`r paste("#'    html_document")`
`r paste("#' ---")`
Joanne Demmler
  • 1,406
  • 11
  • 31
  • NOTE: This is not a YAML problem! The correct YAML output is displayed in the third code box. The problem is rendering the R Studio Snippet to display the YAML code correctly! -> it is a problem with the R Studio snippet! – Joanne Demmler Oct 24 '16 at 08:46
  • Code box number 3 works correctly, but the problem is for the R Studio snippet to create line 4 in box number 3! – Joanne Demmler Oct 24 '16 at 08:59
  • It is more related to this problem, but with one more level of complexity http://stackoverflow.com/questions/20409172/how-to-display-verbatim-inline-r-code-with-backticks-using-rmarkdown – Joanne Demmler Oct 27 '16 at 09:58
  • 1
    No idea if it'll help, hence just a comment rather than an answer, but maybe you can use the fact that backtick is `hex 80` in the ASCII table and slip something like `\x80` or somesuch in. – Mark Setchell Oct 28 '16 at 08:48
  • doesn't `\`r paste("#\' date: '\x60Sys.time()\x60")\`` work? your paste command was also interrupted because of your unbalanced quotes – rawr Oct 29 '16 at 23:13
  • Have you tried using cat()? – Gaius Augustus Oct 31 '16 at 22:26
  • `rawr` great, that was nearby the correct answer. ```r paste("#\' date: '\x60r Sys.time()\x60'")``` - within slanted apostrophes (how did you manage to get those displayed?) – Joanne Demmler Nov 02 '16 at 08:38
  • @JoanneDemmler you need to escape them as in `?\\`if\\`` should display as `?\`if\`` (I think that was your question) – rawr Nov 02 '16 at 17:59

2 Answers2

2

The correct answer was posted by rawr in the comments (he only missed r and a tick mark):

snippet spin.header
`r paste("#' ---")`
`r paste("#' author: 'ENTER'")`
`r paste("#' title: 'ENTER'")`
`r paste("#\' date: '\x60r Sys.time()\x60'")`
`r paste("#' output:")` 
`r paste("#'    html_document")`
`r paste("#' ---")`
Joanne Demmler
  • 1,406
  • 11
  • 31
0

One option is to break the token used during snippet processing for inline R.

snippet sh
  #' ---
  #' author: '${1:AUTHOR}'
  #' title: '${2:TITLE}'
  #' date: '`${3:}r Sys.time()`'
  #' output:
  #'    html_document
  #' ---
  ${0}

Another option is to avoid the inline R parsing completely.

snippet sh
  `r paste0(readLines("~/.R/snippets/spinheader.txt"),collapse = '\n')`

spinheader.txt

#' ---
#' author: '${1:AUTHOR}'
#' title: '${2:TITLE}'
#' date: '`r Sys.time()`'
#' output:
#'    html_document
#' ---
${0}
Thell
  • 5,883
  • 31
  • 55