我不是一个R用户,但我试图生成一些基准信息的各种电脑,我管理,以通知即将到来的购买。
我在命令行上使用R (versiona 3.2.3),并在R中键入以下内容,但这不会在R. Note中产生任何结果-- rbenchmark包已经安装。
任何建议或想法都将不胜感激!谢谢!
> source("rbenchmark_ex.R")
Loading required package: rbenchmark
>rbenchmark_ex.R文件:
require('rbenchmark')
benchmark(1:10^6)
# Example 1 ------
# Benchmarking the allocation of one 10^6-element numeric vector,
# by default replicated 100 times
benchmark(1:10^6)
# simple test functions used in subsequent examples
random.array = function(rows, cols, dist=rnorm)
array(dist(rows*cols), c(rows, cols))
random.replicate = function(rows, cols, dist=rnorm)
replicate(cols, dist(rows))
# Example 2 ----------
# Benchmarking an expression multiple times with the same replication count,
# output with selected columns only
benchmark(replications=rep(100, 3),
random.array(100, 100),
random.array(100, 100),
columns=c('test', 'elapsed', 'replications'))
# Example 3 ---------
# Benchmarking two named expressions with three different replication
# counts, output sorted by test name and replication count,
# with additional column added after the benchmark
within(benchmark(rep=random.replicate(100, 100),
arr=random.array(100, 100),
replications=10^(1:3),
columns=c('test', 'replications', 'elapsed'),
order=c('test', 'replications')),
{ average = elapsed/replications })
# Example 4
# Benchmarking a list of arbitrary predefined expressions
tests = list(rep=expression(random.replicate(100, 100)),
arr=expression(random.array(100, 100)))
do.call(benchmark,
c(tests, list(replications=100,
columns=c('test', 'elapsed', 'replications'),
order='elapsed')))发布于 2016-02-27 11:40:06
如果您源文件,默认情况下不会打印任何文件。有几种方法可以解决这个问题,具体取决于您到底想要什么。
R控制台中的输出
如果要强制打印一些内容,可以将这些内容包装在print()命令中。示例:
print(benchmark(1:10^6))如果要打印所有内容,那么还可以为source()函数提供进一步的参数。有三种有用的可能性:
source("rbenchmark_ex.R", echo = TRUE):这与被求值的代码相呼应,并在每一行代码之后打印结果。基本上,这看起来像是要从脚本输入到控制台的每一行并对其进行计算。source("rbenchmark_ex.R", print.eval = TRUE):它只打印结果,但不回显源代码。source("rbenchmark_ex.R", echo = TRUE, print.eval = FALSE):这只会重复代码,但不会打印结果。(也许不太有用.)输出到文件
也许你宁愿把输出放在一个文件中。这可以通过使用sink()来完成(正如42-在他的评论中所建议的)。只需将以下行添加到脚本中,随后的所有输出都将写入该文件:
sink("output.txt")然后,您可以在R控制台中获取脚本,仍然可以使用上面描述的选项来指定您想要的输出类型。
使用命令行
您也可以直接从命令行执行此操作,而不必先启动R控制台。例如:
Rscript -e 'source("rbenchmark_ex.R", echo = TRUE)'如果在脚本中使用了>,这将将输出写入控制台(当然可以从控制台用sink()重定向)或文件。您也可以直接运行
Rscript rbenchmark_ex.R但这只会打印结果,而不会回显代码。
https://stackoverflow.com/questions/35666532
复制相似问题