Parametrized add_trace in plotly

Hi guys.
I am playing with fully functional modules for plotting (in shiny).
This is a parametrized root function:

library(dplyr)
data(iris)

f = function(data, groupBy, variables, functions){
  data %>% 
    group_by_at(groupBy) %>% 
    summarize_at(vars(variables), functions)
}

f(data = iris, groupBy = "Species", variables = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), functions = list(mean))

I can go even further and parametrize plotly plots like this:

library(plot_ly)

f = function(data, groupBy, variables, functions, xaxis, yaxis, trace){
  data %>% 
    group_by_at(groupBy) %>% 
    summarize_at(vars(variables), functions) %>% 
    plot_ly(x = xaxis, y = yaxis, type = trace)
}


f(data = iris, 
  groupBy = "Species", 
  variables = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), 
  functions = list(mean),
  xaxis = ~Species,
  yaxis = ~Sepal.Length,
  trace = "bar")

The problem I am facing is programmatically adding multiple traces. I have tried map, but it (of course) loops over traces, without plotting them in a one figure.

library(purrr)

data(iris)

f = function(data, groupBy, variables, functions, xaxis, yaxis, trace){
  p = data %>% 
    group_by_at(groupBy) %>% 
    summarize_at(vars(variables), functions) %>% 
    plot_ly(x = xaxis, type = trace)
  map(yaxis, ~add_trace(p, y = .x))
}


f(data = iris, 
  groupBy = "Species", 
  variables = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), 
  functions = list(mean),
  xaxis = ~Species,
  yaxis = c(~Sepal.Length, ~Sepal.Width),
  trace = "bar")

Ï was thinking about instantiating the plot as R6 class and modifying it with add_trace, but I have no experience with OOP and maybe I am overcomplicating things.