Fernandez
Fernandez

Reputation: 259

summary() function gives strange results with Knitr/RStudio

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

Upvotes: 3

Views: 2550

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

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:

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 ;)

Upvotes: 2

Related Questions