MichiZH
MichiZH

Reputation: 5817

R: Saving plots - Cannot open image while still in RStudio

I'm saving several plots in a loop to png files. This works great, as soon as I've closed R. As long as I'm in RStudio, all files have a size of 0 Kb and cannot be opened since they are already in use by another program. Why is that? Did I Forget to code something?

Here's my loop:

plotBasketAndSaveToDirectory<-function(BasketName, SaveToDirectory){
    BasketVariables <- readVariablesFromBasket(BasketName)

    for(i in 1:nrow(BasketVariables)){
      VariableCSV<-read.table(getVariableCSVPath(BasketVariables[i,1]),header=TRUE,stringsAsFactors=FALSE,sep=",")
      VariableCSV$Date<-as.Date(as.character(VariableCSV$Date), format="%Y/%m/%d")
      VariableXTS<-xts(VariableCSV$Close, order.by=VariableCSV$Date)
      png(file=paste(SaveToDirectory,BasketVariables[i,1],".png",sep=""))
      plot(VariableXTS, main=BasketVariables[i,1])
      dev.off
    }
}

Upvotes: 0

Views: 3287

Answers (1)

Harold Cavendish
Harold Cavendish

Reputation: 909

In order to formally end this question, I am adding my comment as an answer:

dev.off() is a function and must be followed by round brackets.

Upvotes: 4

Related Questions