Knitr: print text from code block as R markdown

爱⌒轻易说出口 提交于 2020-05-11 04:44:04

问题


I have the following R Markdown document:

---
title: "Test"
output: html_document
---

```{r cars, echo=FALSE}
myCondition <- TRUE
if(myCondition) {
  print("## Car Summary")
}
summary(cars)
```

When I Knit it to HTML, the "Car Summary" header is rendered in "terminal-like" monospaced font as this:

## [1] "## Car Summary"

But I want it rendered as a header. How do I achieve this?


回答1:


This should work for you:

```{r cars, echo=FALSE, results='asis'}
myCondition <- TRUE
if(myCondition) {
  cat("## Car Summary")
}
```

```{r, echo=FALSE}
summary(cars)
```

Note that the option results = 'asis' is important to print the header. Also note that print() will not work, but cat().



来源:https://stackoverflow.com/questions/44670712/knitr-print-text-from-code-block-as-r-markdown

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