【发布时间】:2015-06-10 17:09:21
【问题描述】:
我有一个跨越数年的数据集。我闪亮的应用程序旨在让用户一次完成一周,进行一些分析并使用该周的新信息更新数据集。例如时间表。
在普通 R 中,我执行以下代码,其中 update_schedule 是自定义函数:
data[indx:(indx+7)] <- update_data(data[indx:(indx+7)], schedule)
使用闪亮,我正在尝试做类似的事情,但得到错误:
.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)
#ui
shinyUI(pageWithSidebar(
sidebarPanel(
actionButton("nextButton", "Process Next Week")
),
mainPanel(
textOutput('week'),
plotOutput('myplot')
)
))
#server
data <<- data.frame(value = rep(1:4, 3:6), open = rep(c(1,1,0), 6))
shinyServer(function(input, output) ({
week <- reactive({as.numeric(input$nextButton)+1})
output$week <- renderText({week()})
#this is the problem line
data[(week()*7):((week()+1)*7)]$open <<- rep(1,7)
#but here the sub-setting works for plotting
output$myplot <- renderPlot({
p1 <- plot(data$value[(week()*7):((week()+1)*7)],
col = as.factor(data$open)[(week()*7):((week()+1)*7)])
return(p1)
})
})
)
有没有办法使用 week() 值来选择要更新的部分数据?此外,实际上我正在尝试一次更新多个变量,而不仅仅是本示例中的“打开”列。
我尝试了以下方法以将我的反应变量保留在反应语句中,但出现错误:
data <<- reactive({data %>%
mutate(open = replace(open, (week()*7):((week()+1)*7, rep(1,7))})
Error in plot(data()$value[(week() * 7):((week() + 1) * 7)], col = as.factor(data()$open)[(week() * :
error in evaluating the argument 'x' in selecting a method for function 'plot': Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of class "reactive"
【问题讨论】: