Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

output markdown in r code chunk

Tags:

r

r-markdown

I have a R markdown file that I want to output rmarkdown from the script itself. For example, I would have the following simple code in an Rmd file.

---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
paste("## This is a Heading in Code")
summary(cars)
```

I want "This is a Heading in Code" to render in rmarkdown. There is a solution in an R script to generate markdown as per http://rmarkdown.rstudio.com/r_notebook_format.html. But I am trying to figure out how to do this in a Rmarkdown file. Any help appreciated. Thanks.

like image 494
yindalon Avatar asked Sep 06 '15 00:09

yindalon


People also ask

How do I show output in R markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do I hide code output in R markdown?

Hide source code: ```{r, echo=FALSE} 1 + 1 ``` Hide text output (you can also use `results = FALSE`): ```{r, results='hide'} print("You will not see the text output.") ``` Hide messages: ```{r, message=FALSE} message("You will not see the message.") ``` Hide warning messages: ```{r, warning=FALSE} # this will generate ...


2 Answers

Why build the header markup (either in markdown or HTML) manually? Try inline R expressions or some helper functions in pander (to generate markdown programatically):

---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## `r 'This is a Heading in Code'`

```{r title, results='asis'}
library(pander)
pandoc.header("This is a Heading in Code", level = 2)
```

```{r cars, results='asis'}
summary(cars)
```
like image 199
daroczig Avatar answered Sep 28 '22 22:09

daroczig


I searched for a good answer for this for some time after using cat("## Heading") inside results='asis' code chucks. I have seen many people dissatisfied by the results='asis' setting in the code chunk because it sets all results of the code chunk to not be wrapped in a code markup block. We have many cases when we want to output the heading along with results that should be wrapped in markup (e.g. a kable table that renders to a html table).

Here is the solution I found by simply specifying the "asis" attribute to the text object with knitr::asis_output and keeping the code chunk in the default 'markup' setting.

---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars}
knitr::asis_output("## This is a Heading in Code")
summary(cars)
knitr::kable(summary(cars))
``` 

enter image description here

Unfortunately, at the current time knitr::asis_output only works in top-level R expressions, and it will not work when it is called inside another expression, such as a for-loop.

like image 23
Claudiu Papasteri Avatar answered Sep 28 '22 20:09

Claudiu Papasteri