如何在降雪中输出消息?

时间:2012-01-14 05:55:37

标签: r snowfall

我正在使用Windows 7上的降雪软件包进行模拟研究。

我喜欢将每10次运行的消息打印到主R控制台以监控进度,但它没有这样做。即。什么都没有打印

非常感谢任何帮助。

runsim = function(nsim,n,mean,var){
cov = 0
for(i in 1:nsim){
if ( i %% 10==0) 
cat("\n Running simulation",i)
dat = function1(n,mean,var)

cov = ...
}
cov / nsim
}
sfExport("function1","runsim")
sfLibrary(library1)

wrapper = function(n){
runsim(100,n,0.5,0.25)
}

Out<-sfLapply(1:100,wrapper)

1 个答案:

答案 0 :(得分:10)

检查?sfCat并找到它所在的行:

  

sfCat是一个在所有从站上打印消息的调试功能(出现在日志文件中)。

这告诉我们需要启用对sfInit(参数slaveOutfile)的调用的登录。然后,每次调用sfCat()都会将内容转储到您指定的日志文件中。我花了一段时间来弄清楚这一点;-)

代码示例

if (!require("snowfall")) {
    install.packages("snowfall")
    require("snowfall")
}

sfInit(parallel=TRUE, cpus=4, slaveOutfile="test.txt")
sfLibrary("snowfall", character.only=TRUE)

res <- sfClusterApplyLB(1:100, function(x) {
    sfCat(paste("Iteration ", x), sep="\n")
})
sfStop()

./test.txt

的输出
[...]

Calling a snowfall function without calling 'sfInit' first or after sfStop().
'sfInit()' is called now.
snowfall 1.84 initialized: sequential execution, one CPU.

Iteration  4
Type: EXEC 
Iteration  92
Type: EXEC 
Iteration  94
Type: EXEC 
Iteration  96
Type: EXEC 
Iteration  98
Type: EXEC 
Iteration  100
ype: EXEC 
Iteration  29
Type: EXEC 
Iteration  31
Type: EXEC 
Iteration  33
Type: EXEC 
Iteration  35
Type: EXEC 
Iteration  37
Type: EXEC 
Iteration  39
Type: EXEC 
Iteration  41
Type: EXEC 
Iteration  43
Type: EXEC 
Iteration  45
Type: EXEC 
Iteration  47
Type: EXEC 
Iteration  49
Type: EXEC 
Iteration  51
Type: EXEC 
Iteration  53
Type: EXEC 
Iteration  55
Type: EXEC 
Iteration  57
Type: EXEC 
Iteration  59
Type: EXEC 
Iteration  61
Type: EXEC 
Iteration  63
Type: EXEC 
Iteration  65
Type: EXEC 
Iteration  67
Type: EXEC 
Iteration  69
Type: EXEC 
Iteration  71
Type: EXEC 
Iteration  73
Type: EXEC 
Iteration  75
Type: EXEC 
Iteration  77
Type: EXEC 
Iteration  79
Type: EXEC 
Iteration  81
Type: EXEC 
Iteration  83
Type: EXEC 
Iteration  85
Type: EXEC 
Iteration  87
Type: EXEC 
Iteration  89
Type: EXEC 
Iteration  91
Type: EXEC 
Iteration  93
Type: EXEC 
Iteration  95
Type: EXEC 
Iteration  97
Type: EXEC 
Iteration  99
 EXEC 
Iteration  74
Type: EXEC 
Iteration  76
Type: EXEC 
Iteration  78
Type: EXEC 
Iteration  80
Type: EXEC 
Iteration  82
Type: EXEC 
Iteration  84
Type: EXEC 
Iteration  86
Type: EXEC 
Iteration  88
Type: EXEC 
Iteration  90

自己的问题

有没有人知道如何“控制”转到日志文件的具体内容? 例如,最好的方法是包括哪些工人完成哪项工作等信息。