summary()函数使用Knitr / RStudio给出奇怪的结果

时间:2022-07-22 14:55:35

I am currently experiencing some strange problems when processing a simple Markdown script under RStudio. Summary() function gives an incorrect result, and I cannot figure what's happening, because RStudio is not giving any error at all.

在RStudio下处理简单的Markdown脚本时,我目前遇到一些奇怪的问题。 Summary()函数给出了不正确的结果,我无法弄清楚发生了什么,因为RStudio根本没有给出任何错误。

If I execute the following RMarkdown script (I have put the file with the data here)

如果我执行以下RMarkdown脚本(我在这里放了包含数据的文件)

```{r}
load('mydata.rda')
summary(mydata$b)
head(sort(mydata$b))
```
```{r}
sessionInfo()
```

I get the following result

我得到以下结果

load("mydata.rda")
summary(mydata$b)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0    6000   10000   12000   16000   35000

head(sort(mydata$b))
## [1] -0.01 -0.01  0.00  0.00  0.00  0.00

sessionInfo()
## R version 2.15.1 (2012-06-22)
## Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
## 
## locale:
## [1] es_ES.UTF-8/es_ES.UTF-8/es_ES.UTF-8/C/es_ES.UTF-8/es_ES.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_1.0.5
## 
## loaded via a namespace (and not attached):
## [1] digest_0.5.2   evaluate_0.4.3 formatR_0.6    plyr_1.7.1    
## [5] stringr_0.6.1  tools_2.15.1

As you can see, the result is wrong, because the actual minimum value of the 'b' variable is negative, something the summary() execution seems to ignore. I have tried the same with a Knitr Rnw pdf script and it does exactly the same. However, when I run it through Sweave, the result is ok.

如您所见,结果是错误的,因为'b'变量的实际最小值是负数,而summary()执行似乎忽略了这一点。我用Knitr Rnw pdf脚本尝试了相同的操作,它完全一样。但是,当我通过Sweave运行它时,结果还可以。

What is the summary function returning when it is called under knitr/RStudio? Is this a side effect of something I am missing or a bug?

在knitr / RStudio下调用时返回的汇总函数是什么?这是我遗漏的东西还是一个错误的副作用?

Regards, Gus

问候,格斯

1 个解决方案

#1


2  

Try adding the following to the top of your document:

尝试将以下内容添加到文档顶部:

```{r, echo=FALSE}
options(digits = 7)
```

To see what the difference is between an R session and a markdown -> HTML knitr session, type the following in your R console and include it in your markddown document and compare the output of each:

要查看R会话和降价 - > HTML knitr会话之间的区别,请在R控制台中键入以下内容并将其包含在markddown文档中并比较每个输出:

options()

options("digits") in a default R session is 7, but in the environment that the knitting of an HTML document from a markdown file (at least on my system) it is 4. Not sure where that is set though ;)

默认R会话中的选项(“数字”)是7,但是在从markdown文件(至少在我的系统上)编织HTML文档的环境中,它是4.不确定在哪里设置;)

#1


2  

Try adding the following to the top of your document:

尝试将以下内容添加到文档顶部:

```{r, echo=FALSE}
options(digits = 7)
```

To see what the difference is between an R session and a markdown -> HTML knitr session, type the following in your R console and include it in your markddown document and compare the output of each:

要查看R会话和降价 - > HTML knitr会话之间的区别,请在R控制台中键入以下内容并将其包含在markddown文档中并比较每个输出:

options()

options("digits") in a default R session is 7, but in the environment that the knitting of an HTML document from a markdown file (at least on my system) it is 4. Not sure where that is set though ;)

默认R会话中的选项(“数字”)是7,但是在从markdown文件(至少在我的系统上)编织HTML文档的环境中,它是4.不确定在哪里设置;)