Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using backticks in a knitr document

Tags:

r

knitr

I have a simple knitr document:

\documentclass{article}
\begin{document}
<<>>=
`+`
@
\end{document}

which when compiled results in the error:

Error in parse(text=x, srcfile=src)

I was expecting the compiled document to show

R> `+`
function (e1, e2)  .Primitive("+")

R> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_3.0.2
like image 632
csgillespie Avatar asked Nov 19 '13 17:11

csgillespie


1 Answers

The problem is formatR, which is used to tidy up your code before displaying and evaluating it. Your backticks get removed, leaving a bare +, which of course won't evaluate by itself:

tidy.source(text="`+`")
# + 

You can do as joran said and wrap it in print, or you can set tidy=FALSE:

<<tidy=FALSE>>=
`+`
@
like image 179
Peyton Avatar answered Oct 13 '22 07:10

Peyton