编织器:将代码块中的文本打印为R降价 [英] Knitr: print text from code block as R markdown

查看:57
本文介绍了编织器:将代码块中的文本打印为R降价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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)
```

当我将其编织为HTML时,汽车摘要"标头以类似终端的"等宽字体呈现,如下所示:

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?

推荐答案

这应该对您有用:

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

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

请注意,选项results = 'asis'对于打印页眉很重要.另请注意,print()将不起作用,但是cat().

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

这篇关于编织器:将代码块中的文本打印为R降价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆