output markdown in r code chunk

帅比萌擦擦* 提交于 2019-12-20 14:20:17

问题


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.


回答1:


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)
```


来源:https://stackoverflow.com/questions/32418860/output-markdown-in-r-code-chunk

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