0

I wanna save a list of scatterplots with their filenames. Therefor I give each plot a name, which worked perfectly:

names(scatterplot_standardcurve) <- 
  sub("\\.xlsx$", 
      ".png", 
      names(standardcurve_concentration))
> print(scatterplot_standardcurve)
$K_20210722

$A_20210722

$c_20210722

$d_20210722

$t_20210722

$v_20210722

And then I want to save them in a specific folder but I always get an error

lapply(names(scatterplot_standardcurve), 
       function(nm) print(scatterplot_standardcurve[[nm]]) + 
         ggsave(filename = file.path("Z:/output/scatterplot_standardcurve/", 
                                     nm )))
Error: Unknown graphics device ''
Raphael
  • 29
  • 5
  • This should help: https://stackoverflow.com/a/34245191/4982645 – CzechInk Aug 05 '21 at 00:05
  • Does this answer your question? [Saving plots within lapply](https://stackoverflow.com/questions/34241954/saving-plots-within-lapply) – CzechInk Aug 05 '21 at 00:06
  • 1
    Your code suggests that you are replacing `xlsx` with `png` while assigning the names but when you do `print(scatterplot_standardcurve)` there is no `'.png'` in the name. – Ronak Shah Aug 05 '21 at 04:04

1 Answers1

0

Using imap you'll be able to iterate over the ggplot object as well as it's name. Try -

library(purrr)
library(ggplot2)

imap(scatterplot_standardcurve, 
      ~ggsave(sprintf("Z:/output/scatterplot_standardcurve/%s.png", .y), .x))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213