I am trying to add a plot in r shiny app using plotly. I can add it when I add the traces manually. But now I want to add the traces dynamically. Also I dont want to add traces for all columns. I just want to add traces for columns which end with sale. Below is the code so far. This doesnt even add all the traces. How can I add all the traces which just which have _sales
in them.
output$pacingplot <- renderPlotly({
colNames <- names(Delivery_data)[-1] #Assuming Date is the first column
print(colNames)
p <- plotly::plot_ly(x = ~Delivery_data$Date, type = "scatter",
mode = "lines")
for(trace in colNames){
p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p %>%
layout(title = "Impressions Over Time",
xaxis = list(title = "Date"),
yaxis = list (title = "Impressions"))
})
Below are the colnames (This doenst include date since I removed the first column):
[1] "apples_sales" "apples_count" "bananas_sales" "bananas_count" "oranges_sales" "oranges_count" "peach_sales" "peach_count"