Joanne Demmler Joanne Demmler - 1 month ago 6
R Question

How to output literal backticks in knitr::spin

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("#' ---")`

Answer

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("#' ---")`
Comments