在Shiny中的For循环内部反应函数 [英] For loop inside reactive function in Shiny

查看:225
本文介绍了在Shiny中的For循环内部反应函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Shiny服务器中遇到了for循环的问题,到目前为止,没有人能找到解决方法.我已经为此工作了好几天,但仍然没有任何进展.长话短说,请只看下面的代码.我试图从Google财经中获取数据并计算出平滑后的方差.

I met a problem with for loop in Shiny server and no one can find out how to fix it till now. I have been working on this for days but still didn't get any progress. Long story short, please just look at the following code. I was trying to fetch data from Google Finance and calculated the smoothed variance.

  stock2 <-
    reactive(
      getSymbols(
        toupper(input$ticker2),
        from = as.Date(input$date2[1]) - 150,
        to = input$date2[2],
        src = "google",
        auto.assign = F
      )
    )

  stock3 <- reactive(as.data.table(stock2()))
  stock <- reactive(as.data.frame(stock3()))
  stock.return <- reactive(diff(log(stock()[, 5])))
  stock.mu <- reactive(mean(stock.return()))
  stock.var <- reactive((stock.return() - stock.mu()) ^ 2)
  stock.var.smoothed <- reactive(rep(0, length(stock.return())))

上面的代码完全可以正常工作,我已经测试过了.

Code above works totally fine and I have tested that.

问题是下面的代码,我想根据一些计算来分配向量,但我不知道正确的方法.

So the problem is the following code, I want to assign a vector based on some calculation but I don't know the proper way to do it.

  stock.var.smoothed <- reactive({
    for (i in 2:length(stock.var())) {
      stock.var.smoothed[1] <<- stock.var()[1]
      stock.var.smoothed[i] <<-
        (1 - input$alpha) * stock.var.smoothed()[i - 1] + input$alpha * stock.var()[i]
    }
  })

for循环根本不起作用. 错误代码是

The for loop doesn't work at all. The error code is

错误:"closure"类型的对象不可子集化

Error: object of type 'closure' is not subsettable

但是我已经在函数中使用了()作为变量.所以我不知道该如何解决.

But I already used () for the variables inside the function. So I have no idea how to fix that.

顺便说一句,我也尝试了以下代码

BTW, I also tried the following code

for (i in 2:length(stock.return())) {
  stock.momentum.smoothed[1] <- reactive(stock.momentum()[1])
  stock.momentum.smoothed[i] <-
    reactive((1 - input$beta) * stock.momentum.smoothed()[i - 1] + input$beta * stock.return()[i])
}

错误代码是

如果没有活动的响应上下文,则不允许进行操作. (您尝试做只能从反应式表达式或观察器内部完成的操作.)

Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

我感谢任何可以提供帮助的人.谢谢!

I appreciate anyone that could give any help. Thank you!

推荐答案

您对反应式(stock.var.smoothed[1] <<-)的子集分配根本没有道理:即使没有子集,此操作也不会是您想要的(它将用非反应性值替换您的反应性对象;即它将不再是反应性的.)

Your subset assignment to the reactive (stock.var.smoothed[1] <<-) simply doesn’t make sense: this operation wouldn’t be what you want, even without the subsetting (it would replace your reactive object with a non-reactive value; i.e. it would stop being reactive).

可以创建并分配给反应式表达式中的变量.但不要分配给全局环境(即,不要使用<<-),也不要尝试重新分配反应对象本身.而是创建一个本地临时变量:

You can create and assign to variables inside your reactive expression. But don’t assign to the global environment (i.e. don’t use <<-), and don’t try to reassign the reactive object itself. Instead, create a local temporary variable:

stock.var.smoothed <- reactive({
  value <- numeric(length(stock.var()))
  value[1] <- stock.var()[1]

  for (i in 2 : length(stock.var())) {
    value[i] <- (1 - input$alpha) * value[i - 1] + input$alpha * stock.var()[i]
  }

  value
})

在这里,value可以是任何名称(包括stock.var.smoothed —但这将是一个与变量不同的 变量,因为它的作用域不同).

Here, value could be any name (including stock.var.smoothed — but this would be a different variable than your reactive, because it’s in a different scope).

此外,我非常确定此代码编写时没有循环和临时变量(但对我来说看起来并不立刻).

Furthermore, I’m pretty sure that this code be written without a loop and temporary variable (but it’s not immediately obvious how it would look like to me).

最后,请注意代码风格:不要在变量名称中使用..这很令人困惑,因为它也用于S3调度,以分隔方法的通用名称和类名称. R中的一个常见约定是使用下划线(stock_var_smoothed).

Finally, a note on code style: don’t use . inside variable names. This is confusing, since it’s also used for S3 dispatch to separate the method’s generic name and class name. A common convention in R is to use underscores instead (stock_var_smoothed).

这篇关于在Shiny中的For循环内部反应函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆