Skip to content

Cannot add traces in a loop #1357

@aljabadi

Description

@aljabadi

Hi,

I'm just wondering why the following plots turn out differently (I only change 3 numbers to a variable containing them (i). Even when I run the code lines one after another.
Interestingly, if I use different variable names (i,j,k, etc), I get the expected result.

library(plotly)

#### Code 1
df <- data.frame(a1=seq(1,20), a2=seq(21,40), a3=seq(41,60))
p <- df %>% plot_ly(x = ~seq(1,dim(df)[1]))
i=1
p <- p %>% add_trace(y = ~ df[,paste0('a',1)]) 
i=2
p <- p %>% add_trace(y = ~ df[,paste0('a',2)])
i=3
p <- p %>% add_trace(y = ~ df[,paste0('a',3)])
p

#### Code 2
df <- data.frame(a1=seq(1,20), a2=seq(21,40), a3=seq(41,60))
p <- df %>% plot_ly(x = ~seq(1,dim(df)[1]))
i=1
p <- p %>% add_trace(y = ~ df[,paste0('a',i)]) 
i=2
p <- p %>% add_trace(y = ~ df[,paste0('a',i)])
i=3
p <- p %>% add_trace(y = ~ df[,paste0('a',i)])
p

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions