I'm building a function that performs some actions on a dataset, and I want it to return a variety of plots that may or may not be needed at any given time.
My approach to now is to return a list of objects, including some ggplot objects.
The issue is that when I call the function without assignment, or execute the resulting list, the ggplots are plotted alongside the printing of the list summary, a behaviour I want to avoid as the object may include many ggplot objects.
Example:
library(ggplot2)
df <- data.frame(
x = 1:10,
y = 10:1,
g = rep(c('a', 'b'), each=5)
)
df_list <- split(df, df$g)
plot_list <- lapply(df_list, function(d){
ggplot(d) +
geom_point(aes(x=x, y=y))
})
plot_list
# $`a` # plots plot_list$a
#
# $`b` # plots plot_list$b
I don't want to modify the default behaviours of the ggplot2 object, though I am open to a more advanced S3 solution where I have no idea how to go about this.