Vysh
Vysh

Reputation: 738

Parallel processing in R

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).

Upvotes: 1

Views: 727

Answers (2)

Oscar
Oscar

Reputation: 855

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()

Upvotes: 1

Mandar
Mandar

Reputation: 1769

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

Upvotes: 1

Related Questions