1

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

Vysh
  • 718
  • 1
  • 7
  • 20
  • Please show some come, we need a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). How are you saving the plots? Using `ggsave`? – Molx Jul 02 '15 at 14:56
  • # Plotting graphics.off() plotIt(times,result) dev.copy(device=png,filename=str_c(p1,"/",cur,".png"),width = 800, height = 600) dev.off() where plotIt is a function that uses plot(time,output) – Vysh Jul 02 '15 at 15:00
  • Where's `plotIt` from? It's not from `ggplot2`apparently. Shouldn't it be `plotit`? – Molx Jul 02 '15 at 15:03
  • plotIt is a user defined function. The definition essentially tells R to show the plot as it grows. The definition is something like this: { 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="result" ,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) ) – Vysh Jul 02 '15 at 15:05
  • Please add this and other relevant code to the question, it's hard to read here in the comments. – Molx Jul 02 '15 at 15:06
  • I am very sorry. I have added it as you have asked to. Thanks for the quick response! – Vysh Jul 02 '15 at 15:14

2 Answers2

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

Mandar
  • 1,659
  • 1
  • 10
  • 14
  • Thanks for the reply. Added. And sorry, my bad, first time using these forums. I thought both were separate.The thing is, this works perfectly fine and I get what I want when I am doing a simple for loop. However, when I try putting it within foreach and do parallel, I just have empty png files. Not sure how to correct it. – Vysh Jul 02 '15 at 16:51
  • Hi Can you please add code that you are using to generate "cur" ? And add your foreach and doParallel code too. Lets take a look at it :) – Mandar Jul 02 '15 at 21:23
1

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()
Oscar
  • 855
  • 9
  • 13