3

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.

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

```{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.

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?

Regards, Gus

4

1 回答 1

2

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

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

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

options()

options("digits")在默认的 R 会话中是 7,但在从 Markdown 文件(至少在我的系统上)编织 HTML 文档的环境中,它是 4。但不确定在哪里设置;)

于 2013-02-11T11:19:32.767 回答