0

I'm making some visualization using R Studio. I have a list of dataframes:

tickers_df <- read.csv('tickers.csv')
v_df <- split(tickers_df, tickers_df$pair_code)

Now I want to make a plot for each dataframe within v_df in its own Viewer window. I'm doing:

for (pair_df in v_df) {
  col_name <- names(pair_df)[4:9]
  colors <- c('green', 'darkgreen', 'red', 'darkred', 'blue', 'darkblue')
  df_layout <- data.frame(col_name, colors)
  p <- plot_ly()
  for (i in 1:nrow(df_layout)) {
    col_name <- as.character(df_layout[i, 1])
    p <-
      add_trace(
        p,
        x = pair_df$step,
        y = pair_df[, col_name],
        name = col_name,
        type = 'scatter',
        mode = "lines",
        line = list(color = df_layout[i, 2], width = 1)
      )
    p <- layout(p, title = pair_df$pair_code[[1]])
  }
 p
}

But this code doesn't work as expected - it shows no charts at all. How can I draw many plotly charts within a loop? And btw what is the meaning of last line with only p variable? Like in this example:

p <- plot_ly( x = df$time.1, y = df$total_profit, line = list(color = 'darkred'))
p #what is this standing for?
chernish2
  • 113
  • 1
  • 9
  • 1
    the `p` is akin to `print(p)`. you're assining the result of plot_ly() function to p. You're explicitly calling it to display the plot. – infominer May 02 '19 at 20:45
  • and this thread might help: https://stackoverflow.com/questions/36234169/plotly-charts-in-a-for-loop, Also create a reproducible example using https://www.tidyverse.org/help/. And if you can't share the data, make similar looking data with dummy numbers – infominer May 02 '19 at 20:47

0 Answers0