Parallel processing in R

爷,独闯天下 提交于 2020-02-05 01:22:32

问题


I have a code in R to perform classification and estimation( using regression modeling) on 60 data sets using random forest algorithm and at the end of it there is a plot to show how a quantity evolves with time. I am performing leave one out procedure on the same and since it takes a long time, I have used parallel processing using the doSnow package. I am able to see that the code does work properly (I am storing the output of my cat commands in a separate log file). However, when I open the plot saved after each iteration of the foreach loop, it is empty. Seems like a complete waste of time since the plot results were the only output that I was saving. What am I doing wrong here? I am using R-Studio.

The code is :

# Plotting
graphics.off()

plotIt(times,result)
dev.copy(device=png,filename=str_c(p1,"/",cur,".png"),width = 800,  height =  600)
dev.off()

and the definition for the plotIt (userdefined fn) is:

    plotIt = function(times,result)
    {
    par(mar=c(4.1,4.2,0.5,0.5))
    par(mfrow=c(2,1)) 

    t = time[ length(time) ]
    plot(time/60,result
        ,xlab="time (min)"
        ,ylab="output"
        ,xlim=c(min(times)/60,max(times)/60)
        ,ylim=c(0,1)
        ,"s"
        )
    points(t/60,result[length(result)],col="red")
    lines(c(min(times)/60,max(times)/60),c(0.5,0.5),lty=2)
    lines(c(0,0),c(0,1),lty=3)
    }

The plot grows with increasing value of time. As it grows, I am saving each frame. "cur" represents the frame number. Assume my t value goes from 1 to 50, I will have 50 frames with the final frame showing the finished plot. So inside my path(p1) I will have 50 plots (png files).


回答1:


This question has been asked in duplicate on multiple occasions. Please keep it to only one instance.

As far as the answer goes: in place of code below,

filename=str_c(p1,"/",cur,".png")

You may define filename using

filename=paste(p1,"/",cur,".png",sep="")

I am hoping that p1 is the path of the file. In case you are having issue with "/" in the path/file.png please use

graphics.off()
setwd(p1)
png(filename=paste(cur,".png",sep=""),width = 800,  height =  600)
plotIt(times,result)  
dev.off() 

You have not written anything about how "cur" is getting generated. So please include that in your explanation too so that it becomes easier to find problems with the code. Best -Mandar




回答2:


What if you tried opening the png device before making the plot. I suspect that the way you have it plotIt is being sent to a NULL device, hence why you are getting an empty plot

png(filename=str_c(p1,"/",cur,".png"),width = 800,  height =  600)
plotIt(times,result)
dev.off()


来源:https://stackoverflow.com/questions/31187900/parallel-processing-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!