【发布时间】:2020-02-26 05:20:37
【问题描述】:
我正在尝试使用 plotly 在 r shiny 应用程序中添加一个情节。我可以在手动添加痕迹时添加它。但现在我想动态添加痕迹。此外,我不想为所有列添加跟踪。我只想为以销售结尾的列添加跟踪。以下是到目前为止的代码。这不会添加所有痕迹。如何添加所有包含_sales 的痕迹。
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"))
})
以下是列名(这不包括自从我删除第一列以来的日期):
[1] "apples_sales" "apples_count" "bananas_sales" "bananas_count" "oranges_sales" "oranges_count" "peach_sales" "peach_count"
以下是数据
Delivery_data <- data.frame(
Date = c("2019-08-19", "2019-08-20", "2019-08-21",
"2019-08-22", "2019-08-23", "2019-08-24"),
apples_sales = c(10882.05495, 516.29755, 949.4084, 3950.5318,
2034.02055, 1770.50415),
apples_count = c(239575, 11281, 20150, 88679, 45672, 38553),
peach_sales = c(0, 0, 0, 0, 0, 0),
peach_count = c(0, 0, 0, 0, 0, 0),
bananas_sales = c(9643.600102, 6041.538067, 5371.758106, 5238.308826,
4994.43054, 5001.303585),
bananas_count = c(630827, 510219, 565440, 576678, 518081, 551733),
oranges_sales = c(0, 1694.44, 9105.89, 6179.47, 7366.31, 6275.43),
oranges_count = c(0, 684210, 3695182, 2501560, 2984563, 2531400)
)
【问题讨论】:
-
你应该发布一个工作示例。
-
刚刚修改了问题
-
擅长添加数据。你想输入代码来创建一个
output容器吗? (您还应该添加一个library(plotly)行。 -
每个人的痛点都不一样。我不是普通的
plotly-user,所以“创建一个函数作为列表叶”然后“使用它”的风格并不是特别自然。我需要一个完整的用例,即该列表项做什么:output$pacingplot?当我将您的数据扔到一个空列表中时,我添加了一个“pacingplot”元素作为参数,我得到:Error in eval(expr, data, expr_env) : object 'apples_sales' not found