首页 文章

Rmarkdown转换为pdf:显示基本直方图,但之后不显示输出

提问于
浏览
1

下面的代码用于生成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"))

1 回答

  • 1

    发生这种情况是因为您在文档上获得的输出既不是警告,也不是消息,也不是错误 . 打印的那些东西是 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](http://www.inside-r.org/r-doc/base/invisible)和[SO question](https://stackoverflow.com/questions/11653127/what-does-the-function-invisible-do) . 
    
    问候!

相关问题