【问题标题】:Rmarkdown converting to pdf: show base hist plot but not the output afterwardsRmarkdown 转换为 pdf:显示基本历史图,但不显示之后的输出
【发布时间】:2014-09-30 04:07:20
【问题描述】:

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

【问题讨论】:

    标签: r pdf plot r-markdown


    【解决方案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")}))
    ```
    

    参考invisibledocSO question

    问候!

    【讨论】:

    • 阿托斯,谢谢!这确实奏效了。不知道这个功能。又学到了新东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    相关资源
    最近更新 更多