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

I want to evaluate the performance of some code in different versions of R. This is basically quite simple:

  • Run r
  • Use system.time() to measure the time it takes to run a piece of code.
  • Complete R
  • Rinse and repeat in another version

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

How to do it?


Sample code for knitr for demonstration This code depicts graphics using ggplot , but it is obvious that both versions return the same timings since I don’t know how to launch a new version of R for each fragment.

 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) }) ``` 
+8
r knitr
source share
1 answer

Adding Rscript to knitr was easy , but I was restrained by the R error . In any case, this engine is available from version 1.1.5 and will be on CRAN as version 1.2.

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

For large-scale performance comparisons, I think that Ari B. Friedman suggested in the comment above that this is the best way to go. It will be rather tedious to enter engine paths if you have a lot of code fragments for comparison.

+6
source share

All Articles