0

I've created a for-loop that reads individual time series files (.csv) and exports the forecast values for each time series back to csv. I wanted to also export the individual time series plots to JPEG within the for loop. I am new to R and would like to have some guidance on how to do it. Not sure if creating another for-loop within the existing for-loop is the answer.

I've tried exporting a sample plot for one time series and it worked. I couldn't figure out how to do it within the existing for-loop.

Here's the for-loop code (imports/exports forecast values from/to CSV)

setwd("wd")
 for (file in list.files(pattern = "*.csv")) {
     library(prophet)
     df <- read.csv(file)
     m <- forecast(df)
     future <- make_future_dataframe(m, periods = 90)
     out <- predict(m, future)
     write.csv(out, sprintf("out_%s.csv", file))
}

Here's the code that exports a JPEG plot

jpeg('rplot.jpg')
plot(m,forecast)
dev.off()

Edit:

setwd('wd')

files <- list.files(pattern = "\\.csv$")

for (i in seq_along(files)) {
    library(prophet)
    df <- read.csv(files[i])
    m <- prophet(df)
    future <- make_future_dataframe(m, periods = 90)
    forecast <- predict(m, future)
    out <- predict(m, future)
    write.csv(out, sprintf("out_%s.csv", files[i]))

    jpeg(paste('rplot', files[i], '.jpg'))
    plot(m, forecast)
    dev.off()
}
kenkenhimself
  • 71
  • 1
  • 13
  • Your loop reads and predicts from the same file: `file.csv`. Unless some random process occurs in `forecast`, all *out* csv files would maintain the same content including attempted plot. – Parfait May 05 '19 at 16:56
  • By the way, per R tag: please specify all non-base packages with `library()` calls. – Parfait May 05 '19 at 16:57
  • @Parfait, the code above was 'shortened'. it works just fine and exports the forecast values. and yes `library()` should be there too. i'm more curious on the correct implementation for exporting the jpeg plots within the loop. – kenkenhimself May 05 '19 at 17:00
  • Once again, please edit question with exact `library` calls. Regarding plots, simply run inside loop and change the file name as below answer shows which is unclear why it doesn't work per your comment. It must be due to the single `file.csv` you use across all iterations. – Parfait May 05 '19 at 17:07

1 Answers1

1

You need to change filename inside the loop

setwd("wd")

files <- list.files(pattern = "\\.csv$")

for (i in seq_along(files)) {
  df <- read.csv(files[i])
  m <- forecast(df)
  future <- make_future_dataframe(m, periods = 90)
  out <- predict(m, future)
  write.csv(out, sprintf("out_%s.csv", i))

  jpeg(paste0('rplot', files[i], '.jpg'))
  plot(m, forecast)
  dev.off()
}
  • Didn't work, the CSV files are now displaying out_1.csv, out_2.csv, which should have retained the original file name instead of numerical values. And the JPEG files are mostly empty, with the file name retaining the original CSVs (i.e. 'rplot forecast1.csv.jpg') – kenkenhimself May 05 '19 at 16:08
  • 1
    Simply change `sprintf("out_%s.csv", i)` to `sprintf("out_%s.csv", files[i])`. Regarding empty plots, without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), we cannot help further. Your original post reads the **same** file across all iterations where iterated files are only used for naming outputs. – Parfait May 05 '19 at 17:07
  • @Parfait thanks, `files[i]` worked. Also corrected the code from the original post (`file` instead of `file.csv`) and included the library call. Re-ran the whole thing and the plots are still empty. Will check out the link on reproducible example. – kenkenhimself May 05 '19 at 19:28
  • Actually @Reese may have kept using the unknown *forecast* object instead of *future* in `plot` call where *forecast* was used in your original non-loop code. – Parfait May 05 '19 at 20:37
  • @Parfait, i did edit the original post showing Reese's code (with minor edits!), but I still get blank JPEGs. – kenkenhimself May 05 '19 at 21:57
  • Check your *m* and *future* data in each iteration as they may be empty and try printing plot in `for` loop: `print(plot(...))`. Again, all guesses since we have no reproducible example on our end. See other R questions on how they provide sample data (hint: `dput`). – Parfait May 05 '19 at 23:51