简体   繁体   English

Knitr:从代码块打印文本为R markdown

[英]Knitr: print text from code block as R markdown

I have the following R Markdown document: 我有以下R Markdown文档:

---
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: 当我将其编织为HTML时,“Car Summary”标题以“类似终端”的等宽字体呈现,如下所示:

## [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. 请注意,选项results = 'asis'对于打印标题很重要。 Also note that print() will not work, but cat() . 另请注意, print()不起作用,但cat()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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