我要评估的不同版本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)
})
```
难道你不可能只是有一个'bash'脚本或者不能完成R会话的所有开始/停止,每个会话都会将其结果写入文件,然后使用'knitr'来读取所有日志文件并总结结果?不够优雅,但可能更准确,更容易。 – 2013-03-07 12:40:20
@ AriB.Friedman是的,可能。但这听起来像工作! – Andrie 2013-03-07 12:47:50
估计你需要使用'system()'来运行另一个R进程... – Spacedman 2013-03-07 15:17:58