简体   繁体   中英

Knitr: print text from code block as R markdown

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?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM