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

2019-01-22 01:39发布

问题:

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

回答1:

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.



回答2:

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


标签: r markdown knitr