我正在尝试让这个反应式返回一个数据框,以便我可以用Plotly进行操作。
avghour <- reactive({
result <- data.frame()
start_date <- as.numeric(unlist(input$i6[1]))
end_date <- as.numeric(unlist(input$i6[2]))
mkw <- maxkwpeakdates[(maxkwpeakdates >= start_date & maxkwpeakdates <=
end_date) & !is.na(maxkwpeakdates), ]
mkw <- na.omit(mkw)
mopkw <- maxonpeakkwdates[(maxonpeakkwdates >= x &
maxonpeakkwdates <= y) & !is.na(maxonpeakkwdates),
]
mopkw <- na.omit(mopkw)
mkwhour <- data.frame(as.data.frame(apply(mkw, 2, hour)))
mopkwhour <- as.data.frame(apply(mopkw, 2, hour))
mkwhour <- as.data.frame(sapply(mkwhour, tabulate, 24))
mopkwhour <- as.data.frame(sapply(mopkwhour, tabulate, 24))
mkwhour <- as.data.frame(apply(mkwhour, 1, mean))
mopkwhour <- as.data.frame(apply(mopkwhour, 1, mean))
result <- data.frame(mkwhour, mopkwhour)
colnames(result) <- c("1", "2")
return(result)
})
我希望能够绘制出结果数据框的图形。当我调试我的应用程序时,发现avghour保存的是一个函数而不是数据框。虽然反应式所需的值强制反应式正确更新,但我无法将结果保存为数据框。我是否正确使用了反应式?我需要以另一种方式完成这个任务吗?
更新
I have changed my server code to be like this:
server <- function(input, output) {
averages <- reactiveValues(hourly = avghour(input$'i6'[1], input$'i6'[2]))
observeEvent(input$submit,{
averages$hourly <- avghour(input$'i6'[1], input$'i6'[2])
})
output$"1" <- renderPlotly({
plot_ly() %>%
add_trace(type = 'bar',
data=averages$hourly,
x=~'1',
y=~'2')
})
}
getHourlyAverage是我之前的同一个函数。现在我遇到了错误:没有活动的反应性上下文,不允许操作
。我该如何使用observeEvent和reactiveValues来处理用户输入?
avghour()
调用它,而不是avghour
。 - Tom StringhamgetHourlyAverage
。 - Mike Wise