I want to evaluate the performance of some code in different versions of R. This is easy enough in principle:
system.time()
to measure the time it takes to run a piece of codeNow, 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)
})
```
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With