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

前端 未结 2 626
孤街浪徒
孤街浪徒 2020-12-08 06:37

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

相关标签:
2条回答
  • 2020-12-08 06:50

    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
    
    0 讨论(0)
  • 2020-12-08 07:02

    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.

    0 讨论(0)
提交回复
热议问题