Search code examples
rlapplysapply

lapply pdf output overwrites pdf instead of creating new ones


I would be glad if someone could help me here. So, I am trying to use lapply for the secondary.peaks (sangeranalyserR package) function, however, I can not find a way to change the file.prefix to something that can change depending on the file. Here is what I am trying and failing.

lapply(list, secondary.peaks, output.folder = "/secondary_peaks",
   file.prefix = names(list))

So, basically the problem is in file.prefix, I want that for every file in the list uses the name of the list. The problem is when I make a list of names, it basically just take the first name and repeat it to every file in the list. Thus instead of end up with 5 files, with 5 names, I ended up with 1 file with the first name of the list names.


Solution

  • lapply only iterates over the object given in its X argument. To iterate over multiple arguments, you need to use mapply. Take note that the argument order in mapply is a little different. I would try (untested)

    mapply(FUN = secondary.peaks, 
           s = list, 
           file.prefix = names(list), 
           MoreArgs = list(output.folder = "/secondary_peaks"))
    

    And if you want to be sure you get a list back as output, you can set SIMPLIFY = TRUE in the mapply call. Make sure to read over the documentation for mapply.