我有以下问题,希望你能帮助我:
我使用了plotly,有光泽和RStudio,我希望实现以下目标:
我已经设法这样做并且它正在工作,但它工作得非常慢!指向某处,等待3秒,构建直方图,指向其他地方,再次等待,生成直方图,依此类推。到目前为止它只具有非常基本的功能。它有没有机会更顺畅,更快捷?我应该尝试用Python或其他语言来做吗?
以下是代码。您可以从https://github.com/ymra/dynamic_histogram/blob/master/dji.csv下载示例数据。此外,您必须知道直方图是为650次观察生成的(从鼠标点开始并返回)。前650个观测值(从0到650)是NA。
library(shiny)
library(plotly)
library(moments)
ui <- fluidPage(
h2("Prices and return rate"),
plotlyOutput("plot"),
h2("Histogram"),
h3("move mouse over line line plot"),
plotlyOutput("plot2")
)
server <- function(input, output) {
# Read and transform data
alltogether <- data.frame(read.csv(file = "./dji.csv", sep = ",", header = TRUE))
#change column names to simplify
colnames(alltogether) <- c("date","returns","prices")
# Generate statistic magic values
for (i in 3000:650){
n = i-649
alltogether$means[i] <- mean(alltogether$prices[n:i])
}
# render
output$plot <- renderPlotly({
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
range = c(-0.14,0.3)
)
p <- plot_ly() %>%
add_lines(data = alltogether, x = ~date, y = ~prices, name = "price", text = ~paste("mean: ", alltogether$means)) %>%
add_lines(data = alltogether, x = ~date, y = ~returns, name = "return", yaxis = "y2") %>%
layout(title = "Price and return rate", yaxis2 = ay)
})
output$plot2 <- renderPlotly({
d <- event_data("plotly_hover")
b <- d$pointNumber[1]
e <- b+649
rangy <- (alltogether$returns[b:e])
if (b > 649){
plot_ly(x=rangy,type = "histogram")
}
})
}