Search code examples
rmapply

R: mapply outputs matrix instead of function output


In R, I have a function (plot.volcano.norm) that plots data from a data frame with ggplot2. It requires two inputs: The data frame with the data and a character vector. Because I have multiple data frames that I want to plot data from, I have multiple data frames in a list (data.list), as well as a vector (title.list) that contains character elements that are needed for the function to work properly (one for each data frame).

When I run the following:

plot.volcano.norm(data.list[[1]],title.list[1])

I get the correct plot for the first data frame in data.list. Now, I want to run the function for all of the elements in the data.list. For this, I used

mapply(plot.volcano.norm, data.list, title.list)

But now the output is a matrix:

            [,1]    [,2]   
data        List,12 List,16
layers      List,3  List,3 
scales      ?       ?      
mapping     List,2  List,2 
theme       List,93 List,93
coordinates ?       ?      
facet       ?       ?      
plot_env    ?       ?      
labels      List,6  List,6 
guides      List,2  List,2

I have checked ?mapply, but I don't know what I am doing wrong. How can I get mapply to work properly? Or do I need to use something else?


Solution

  • By default, SIMPLIFY = TRUE which outputs a matrix. Try:

    mapply(plot.volcano.norm, data.list, title.list, SIMPLIFY = FALSE)