Is there anyway to use a for loop to define plot_ly layout

I want to use a for loop from a shinyapp to define multiple y axes in my plotly graph. Is this possible in r?

#example of dataset of plotly to plot multiple y axes in for loop
library(plotly)

len = length(names(mtcars))
names = names(mtcars)

n=0
p= plot_ly()
for (i in names){
n=n+1
p=add_trace(p,data = mtcars, x = mtcars[,“mpg”], y = mtcars[,i], yaxis = paste0(‘y’,ifelse(n==1,’’,n)), type = ‘scatter’, mode = ‘scatter’)
}

n=0

for (i in 1:len)
{n=n+1
layout(p,assign(paste0(‘yaxis’, ifelse(n==1,"",n)),list( overlaying = “y”, side = ifelse((n %% 2) == 0,‘right’,‘left’), title = paste0(n, " y axis")
)) )
}

p