RShiny:没有活动的被动上下文时不允许操作

时间:2017-11-14 16:56:29

标签: r shiny

我到处寻找解决方案,但我无法弄清楚如何摆脱RShiny中的Operation not allowed without an active reactive context.错误。这是我的服务器代码:

library(ggplot2)

# Surprisingly there is no correlation between charged kw and time
s <- data[,14:16]
s <- s[complete.cases(s),]
cor(s$charged_kwh, s$connected_time_hours)

car_types <- data %>% group_by(car) %>% 
  summarise(nrUsers = n_distinct(user_id), n=n()) %>% 
  filter(car!="Unknown", car!= " ") %>% 
  arrange(desc(nrUsers))

# Connected hours vs actually charging hours
carged_cars <- reactive({
  data %>% 
  filter(!is.na(charged_kwh)) %>%
  mutate(charging_time =  charged_kwh / kw_charge_point_speed)
})

filteredData <- carged_cars()[carged_cars()$start_date >= input$cvc_date_diagram[1] & carged_cars()$start_date <= input$cvc_date_diagram[2] &
  carged_cars()$connected_time_hours <= input$cvc_max,]

connTimeVsStart <- ggplot(reactive(filteredData), aes(x=start_date, y=connected_time_hours)) +
  geom_point(shape=16) +
  geom_point(reactive(filterData), aes(x=start_date, y=charging_time), col='red') +
  ggtitle("Charging time")

output$connTimeVsStart <- renderPlot({
  d <- data()
  connTimeVsStart
})

data这里是一个包含有关电动汽车充电行为的数据的数据框。控制台说我的脚本在此服务器脚本的第20行崩溃,因此定义了filteredData的部分。我觉得我已经尝试过使用reactive({}) vs reactive()等来反复制作源变量的各种组合。有人能发现我做错了吗?

1 个答案:

答案 0 :(得分:0)

也许这段代码可以帮到你。

NB:
 1 /我看不到car_types的任何用途  2 /我不知道您的变量data的创建位置

library(ggplot2)

# Surprisingly there is no correlation between charged kw and time
s <- data[,14:16]
s <- s[complete.cases(s),]
cor(s$charged_kwh, s$connected_time_hours)

car_types <- data %>% group_by(car) %>% 
  summarise(nrUsers = n_distinct(user_id), n=n()) %>% 
  filter(car!="Unknown", car!= " ") %>% 
  arrange(desc(nrUsers))

# Connected hours vs actually charging hours
carged_cars <- reactive({
  data %>% 
  filter(!is.na(charged_kwh)) %>%
  mutate(charging_time =  charged_kwh / kw_charge_point_speed)
})

filteredData <- reactive({
    req(carged_cars())
    req(input$cvc_date_diagram)
    carged_cars()[carged_cars()$start_date >= input$cvc_date_diagram[1] & carged_cars()$start_date <= input$cvc_date_diagram[2] &
        carged_cars()$connected_time_hours <= input$cvc_max,]
})

connTimeVsStart <- reactive({
    ggplot(filteredData(), aes(x=start_date, y=connected_time_hours)) +
        geom_point(shape=16) +
        geom_point(reactive(filterData), aes(x=start_date, y=charging_time), col='red') +
        ggtitle("Charging time")
})

output$connTimeVsStart <- renderPlot({
  connTimeVsStart()
})