How to wrap code and the output in markdown (.Rmd)

瘦欲@ 提交于 2019-12-04 00:25:09

Things have changed since 2015, but FWIW given that you are now using rmarkdown_1.8 and knitr_1.20:

  1. it is handled properly in the default html output;
  2. if you are looking for a pdf output, what you can do is

    • breaking the line to control the code chunk length (if you want to display it) but note that in a character string a \n will be added (in which case you may benefit from using two different code chunks: one for displaying the code another for the outputs);
    • regarding the output, you could set the code chunk option results to 'asis' so the output is handled as if it were a regular piece of text, in which case you can also use paste() or paste0() to use latex tags to tweak how the text is displayed.

So:

1- display the code

{r, eval = F}
output <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
output

2- get the output

{r, results = 'asis', echo = F}
output <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
output

Hope this could help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!