2

I have tried these 3 solutions mentioned in the link Plotly: add_trace in a loop

but my code is still not working, the code shows only the final trace in the loop.

output$plot <- renderPlotly({
    if(input$x == "M"){
      my_x <- c(input$mmin:input$mmax)
      number_of_cpgs <- 500*my_x
      timetaken <- rep(0,input$mmax-input$mmin +1)
      p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines")
      for(i in input$nmin:input$nmax){
        for(j in input$kmin:input$kmax){
          timetaken <- timemat[i,my_x,j]
          p<-add_trace(p, y=~timemat[i,my_x,j], x=~(number_of_cpgs) , type="scatter",mode="markers+lines",visible = TRUE )
        }
      }
    }

enter image description here

Reproducible example:

timemat <- array(c(1:1000), dim=c(10, 10, 10)) 
my_x <- c(1:10) 
number_of_cpgs <- 500*my_x 
timetaken <- rep(0,10) 
p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines") 
for(i in 5:6){ 
  for(j in 6:7){ 
    timetaken <- timemat[i,my_x,j] 
p <- add_trace(p, y=~timemat[i,my_x,j], x=~(number_of_cpgs) , type="scatter", mode="markers+lines", evaluate = TRUE) 
  }
  } 
p
MLavoie
  • 9,277
  • 40
  • 37
  • 54

1 Answers1

1

Is it what you are looking for?

p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines") 
for(i in 5:6){ 
  for(j in 6:7){ 
   # timetaken <- timemat[i,my_x,7] 
  testing = timemat[i,my_x,j]
p <- add_trace(p, y=testing, x=~(number_of_cpgs) , type="scatter", mode="markers+lines", evaluate = TRUE) 
  }
  } 
p

enter image description here

MLavoie
  • 9,277
  • 40
  • 37
  • 54