Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use knitr to compare performance of different versions of R?

Tags:

r

knitr

I want to evaluate the performance of some code in different versions of R. This is easy enough in principle:

  • Start R
  • Use system.time() to measure the time it takes to run a piece of code
  • Terminate R
  • Rinse and repeat in a different version

Now, I want to use knitr to create a report to do this. So, it seems to me I need a mechanism to start a new session in each chunk.

How do I do this?


Some sample knitr markdown code to serve as a demonstration. This code plots a graphic using ggplot, but clearly both versions return identical timings, since I don't know how to start a new version of R for each chunk.

Comparison of R performance
========================================================

# Do analysis in R version 2.14

```{r fig.width=6, fig.height=3}
library(ggplot2)
data(diamonds)

system.time({
  p <- ggplot(diamonds, aes(carat, price/carat, colour=clarity)) + geom_point()
  print(p)
})
```


# Repeat same analysis in R 2.15

```{r fig.width=6, fig.height=3}
library(ggplot2)
data(diamonds)

system.time({
  p <- ggplot(diamonds, aes(carat, price/carat, colour=clarity)) + geom_point()
  print(p)
})
```
like image 269
Andrie Avatar asked Mar 07 '13 12:03

Andrie


1 Answers

Adding the Rscript engine in knitr was easy, but I was held back by an R bug. Anyway, this engine is available since version 1.1.5 and will be on CRAN as version 1.2.

Now you can specify the chunk option engine='Rscript' and engine.path='path/to/the/desired/Rscript'.

For large scale performance comparisons, I think what Ari B. Friedman suggested in the comment above is a better way to go. It will be pretty tedious to type the engine paths if you have many code chunks for comparisons.

like image 190
Yihui Xie Avatar answered Oct 01 '22 16:10

Yihui Xie