r - plotting multiple series on same plot using plotly and lapply in Shiny modules -
i having trouble plotting many data on same plot using lapply
i have module calling , storing dataframes list each lists called dx<-list , each dataframe in list i.e dx[[i]] i'd create add line 1 plot. since dataframes in list have same x axis want add multiple series same plot
i using plotly add_trace , succesfully able following way when dont have modules or list of dataframes
pl<-plot_ly()%>% add_trace(data=dataset1,x=~x,y=~y,mode='lines',type='scatter' ) %>% add_trace(data=dataset2,x=~x,y=~y,mode='lines',type='scatter' ) %>% add_trace(data=dataset3,x=~x,y=~y,mode='lines',type='scatter' ) %>% add_trace(data=dataset4,x=~x,y=~y,mode='lines',type='scatter' ) %>% layout(xaxis = x, yaxis = y)
how can achieve same using lapply here reproducible code
module1ui <- function(id) { ns <- ns(id) wellpanel( selectinput( ns('cars'), "cars:", list("select" = "", "a" = "mazda", "b" = "ford")) ) } module1<-function(input, output, session){ datas = reactive({ if(input$cars=="mazda"){ mtcars$mpg} else( mtcars$disp) }) return(datas) } module2<-function(input, output, session,datas){ datass = reactive({ mtcars$hp }) return(datass) } library(shiny) ui <- fluidpage( sliderinput("slider","how cars?", 1, 10, 1, width = "100%" ), uioutput("selectors"), plotlyoutput("plot1") ) server <- function(input, output, session){ for(i in 1:10) callmodule(module1, i) output$selectors <- renderui({ lapply(1:input$slider, module1ui) }) datas<-list() dx<-list() for(i in 1:10) datas[[i]]<-callmodule(module1,i) pl<-plot_ly() output$plot1<-renderplotly( lapply(1:input$slider, function(i){ df1<-c(seq(1:32)) df2<-datas[[i]]() dx<-cbind.data.frame(df1,df2) #my attempt below doesnt work pl<- add_trace(pl,data=dx[[i]],x=~df1,y=~df2,mode='lines',type='scatter' ,evaluate = true) pl }) ) } shinyapp(ui, server)
Comments
Post a Comment