Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an R Markdown equivalent to \Sexpr{} in Sweave?

Tags:

markdown

r

knitr

Using R Markdown in knitr is there an equivalent to \Sexpr{} in Sweave?

like image 389
Jeromy Anglim Avatar asked May 17 '12 03:05

Jeromy Anglim


People also ask

What is the difference between Markdown and R markdown?

R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

What is knitr in Rmarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

What is R markdown good for?

rmarkdown provides an environment where you can write your complete analysis, and marries your text, and code together into a rich document. You write your code as code chunks, put your text around that, and then hey presto, you have a document you can reproduce.


2 Answers

Yes. You can use

`r your_expression_here` 

So something like

2+2 is: `r 2+2` 

Should produce:

2+2 is: 4 

I initially found it a little difficult trying to figure out what the different syntax was for each of the different styles you could use in knitr (html, markdown, sweave, ...) and resorted to looking at Yihui's minimal examples (which do a good job) but if you can read regular expressions you can view the default pattern definitions. You even have the option of defining your own syntax if you want.

like image 180
Dason Avatar answered Sep 21 '22 03:09

Dason


By default inline R code in R Markdown will be formatted as code. If you just want output to appear asis then inclose the R command in I(...). Enclosing output with I(...) matches the behaviour of Sexpr. This distinction is sometimes important. For more information, see this comment by Yihui Xie. The following table shows the different output from R Markdown to Markdown to HTML.

R Markdown        `r 2 + 2`               `r I(2+2)` Markdown              `4`                     4 HTML             <code>4</code>               4 
like image 31
Jeromy Anglim Avatar answered Sep 22 '22 03:09

Jeromy Anglim