如何使用knitr来比较不同版本R的性能?

And*_*rie 8 r knitr

我想评估一些代码在不同版本的R中的性能.原则上这很容易:

  • 开始R.
  • 使用system.time()测量所花费的时间来运行一段代码
  • 终止R.
  • 冲洗并重复使用其他版本

现在,我想使用knitr创建报告来执行此操作.所以,在我看来,我需要一种机制来在每个块中启动一个新的会话.

我该怎么做呢?


一些示例knitr 降价代码用作演示.此代码使用图形绘制ggplot,但显然两个版本都返回相同的时序,因为我不知道如何为每个块启动新版本的R.

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)
})
```
Run Code Online (Sandbox Code Playgroud)

Yih*_*Xie 6

添加Rscript引擎knitr 很容易,但我被一个R bug阻止了.无论如何,这个引擎从版本1.1.5开始可用,并将在CRAN上作为版本1.2.

现在您可以指定块选项engine='Rscript'engine.path='path/to/the/desired/Rscript'.

对于大规模的性能比较,我认为Ari B. Friedman在上面的评论中提出的是一种更好的方法.如果您有许多用于比较的代码块,则输入引擎路径将非常繁琐.