我使用R中的snow包在多个核心上调用python脚本。我想要的是将进度打印到控制台。在我的函数中使用cat()
,message()
或print()
并未提供任何输出。这使得很难跟踪我的功能进度。
是否可以在clusterApply或clusterMap函数中将输出打印到命令行?
这是我目前的剧本:
library(snow)
library(rlecuyer)
# Files to process
filenames=list.files(dirname(file.choose()),pattern="*.tif$",full.names=T)
# Process function
processfunc=function(filename,filenames){
len_names=length(filenames) #Length of filenames
index = match(filename, filenames) #Index of current file
cat(paste('Processing input files:',format(round(index/len_names*100,2),nsmall=2),'% At:',filename)) # print progress
system(paste('python','D:/pythonscript.py',filename))
}
corenr=7
cl = makeCluster(rep('localhost', corenr), 'SOCK')
clusterExport(cl, list("processfunc"))
clusterEvalQ(cl, library(stringr))
clusterSetupRNG(cl)
clusterMap(cl,function(x,filenames) processfunc(x,filenames),filenames,MoreArgs = list(filenames=filenames))
stopCluster(cl)
答案 0 :(得分:0)
如果通过终端,cmd或powershell运行它,则可以添加一个额外的system
或shell
调用来显示您的字符串。例如:shell(paste('echo', 'your string'))
。
工作示例
library(snow)
library(rlecuyer)
# Files to process
filenames=1:10
# Process function
processfunc=function(filename,filenames){
len_names=length(filenames) #Length of filenames
index = match(filename, filenames) #Index of current file
shell(paste('echo', paste('Processing input files:',format(round(index/len_names*100,2),nsmall=2),'% At:',filename)))
# system(paste('python','D:/pythonscript.py',filename))
}
corenr=7
cl = makeCluster(rep('localhost', corenr), 'SOCK')
clusterExport(cl, list("processfunc"))
clusterEvalQ(cl, library(stringr))
clusterSetupRNG(cl)
clusterMap(cl,function(x,filenames) processfunc(x,filenames),filenames,MoreArgs = list(filenames=filenames))
stopCluster(cl)