r-r标记向下转换为pdf:显示基本历史图,但不显示之后的输出



下面是用R中的基本绘图系统生成直方图并通过Rmarkdown将其转换为pdf(Latex)的代码。我设置了message=FALSE,warning=FALSE,error=FALSE等,但仍然从系统中获得输出。我怎么能只展示情节呢??

---
title: "Test"
author: "Me"
date: "Tuesday, September 30, 2014"
output: pdf_document
---
```{r,message=FALSE,warning=FALSE,cache.comments=FALSE,error=FALSE,prompt=FALSE}
par(mfrow=c(2,2))
replicate(4,hist(replicate(1000,mean(rnorm(10000,1,10))),col="slateblue4",
             main="Distribution of the Mean",col.main="steelblue4",
             xlab="Mean",ylab="Count"))
```

之所以会发生这种情况,是因为您在文档上得到的输出既不是警告、消息也不是错误。打印的那些东西是replicate()函数合法(但不需要)的输出。

最简单的方法是用invisible()函数包裹不方便的replicate(外层)。后者在实践中会抑制传递给它的表达式的输出,从而产生所需的输出。

```{r,message=FALSE,warning=FALSE,cache.comments=FALSE,error=FALSE,prompt=FALSE}
par(mfrow=c(2,2))
invisible(replicate(4,{hist(replicate(1000,{return(mean(rnorm(10000,1,10)))}),col="slateblue4",
             main="Distribution of the Mean",col.main="steelblue4",
             xlab="Mean",ylab="Count")}))
```

invisible:doc与SO问题。

问候!

最新更新