I have a data frame which plots as per below code. But I need this to be reacive/dynamic (basically a loop kind). For example, now we have only 2019, 2020 so that is fine. In case if we have 2017, 2018, 2019, 2020, 2021 and so on. It should take automatically and also there is _cap values(2019_cap) here. If it is _cap values, then mode should be lines+markers
or else lines
. Can we achieve this
asd <- data.frame(week = c(1,1,2,2), year = c("2019","2020","2019","2020"), val = c(1,2,3,4), cap = c(3,4,6,7))
asd_plot <- tidyr::pivot_wider(asd, names_from = year, values_from = c(val, cap), names_glue = "{year}_{.value}")
asd_plot <- as.data.frame(asd_plot)
fig <- plot_ly(asd_plot , x = ~week, y = ~`2019_val`, type = 'scatter', mode = 'lines', name = '2019') %>%
add_trace(asd_plot , x = ~week, y = ~`2020_val`, type = 'scatter', mode = 'lines', name = '2020') %>%
add_trace(asd_plot , x = ~week, y = ~`2019_cap`, type = 'scatter', mode = 'lines+markers', name = '2021') %>%
add_trace(asd_plot , x = ~week, y = ~`2020_cap`, type = 'scatter', mode = 'lines+markers', name = '2021')
fig