Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr inline chunk options (no evaluation) or just render highlighted code

I cannot find information on whether it is possible to specify options for inline chunks in knitr. I've just tried specifying them, as in the regular chunk, but this gives an error.

What I need is to include R code with highlighting in a PDF, but without evaluating it. This can only happen with inline chunks due to the format of the context. Or perhaps there is another way to include highlighted code.

To provide an example, I need something in the lines of:

Some text about something with `r eval=FALSE 1+1` inside the sentence. 

This particular syntax gives:

Error in parse(text = code, keep.source = FALSE) :
<text>:1:11: unexpected ','
1: eval=FALSE,
like image 204
Maxim.K Avatar asked May 06 '13 19:05

Maxim.K


1 Answers

Thanks to Yihui you can do,

\documentclass{article} 
<<setup, include=FALSE>>= 
knit_hooks$set(inline = function(x) { 
  if (is.numeric(x)) return(knitr:::format_sci(x, 'latex')) 
  highr::hi_latex(x) 
}) 
@ 
\begin{document} 

the value of $\pi$ is \Sexpr{pi}, and the function to read a table is 
\Sexpr{'read.table()'}. 

<<test2>>= 
rnorm(10) 
@ 
\end{document} 
like image 138
baptiste Avatar answered Sep 19 '22 15:09

baptiste