【问题标题】:add trace in loop deletes previous traces in R shiny在循环中添加跟踪删除 R 闪亮中以前的跟踪
【发布时间】:2018-11-25 03:25:36
【问题描述】:

我已经尝试过链接中提到的这 3 个解决方案 Plotly: add_trace in a 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 )
        }
      }
    }

可重现的例子:

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

【问题讨论】:

  • 按照答案中的建议,您是否尝试在add_trace 中添加evaluate = TRUE
  • 是的,我也试过了
  • 没有可重复的例子,很难说。
  • 您能否建议使用此代码进行编辑
  • 图书馆(情节)时间垫

标签: r plot ggplot2 r-plotly


【解决方案1】:

这是你要找的吗?

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

【讨论】:

  • 是的,我正在寻找这样的东西,但是如果你看到你已经添加了 5 条跟踪但只添加了 add_trace 循环的最终跟踪
  • 我看到了 4 个不同的痕迹
猜你喜欢
  • 2022-07-01
  • 2019-07-14
  • 2021-11-04
  • 2017-03-23
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
相关资源
最近更新 更多