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,671
  • 41
  • 36
  • 56
DIVANSHU GUPTA
  • 87
  • 1
  • 1
  • 4
  • As suggested in the answer, have you tried to add `evaluate = TRUE` in `add_trace`? – MLavoie Jun 15 '18 at 10:03
  • Yeah i also tried that – DIVANSHU GUPTA Jun 15 '18 at 10:16
  • 2
    Without a reprodicible example, it is hard to tell. – MLavoie Jun 15 '18 at 10:18
  • Can you please suggest edit with this code – DIVANSHU GUPTA Jun 15 '18 at 11:50
  • library(plotly) 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",visible = TRUE ) } } p – DIVANSHU GUPTA Jun 15 '18 at 11:50
  • Your code is working but trace 1 to trace 4 are on top of each other. Unselect some of your trace and you will see it. – MLavoie Jun 15 '18 at 12:01
  • it is no working, it is just changing colours, you can verify by printing timemat[5,8,6] and timemat[6,8,6] both HAVE DIFFERENT VALUES, SO HOW CAN THEY OVERLAP IN PLOT ? – DIVANSHU GUPTA Jun 15 '18 at 12:28
  • In your loop, why is `timetaken <- timemat[i,my_x,j]` is the same as your y, `y=~timemat[i,my_x,j]`? – MLavoie Jun 16 '18 at 10:35
  • i was just trying to use them, to see if data is being overwritten – DIVANSHU GUPTA Jun 18 '18 at 09:33
  • I had the same problem: Removing "~" inside add_trace resolved the issue: `add_trace(p, y=timemat[i,my_x,j], x=(number_of_cpgs) , type="scatter", mode="markers+lines", evaluate = TRUE)` – gianni Mar 15 '19 at 18:37

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,671
  • 41
  • 36
  • 56