简体   繁体   English

如何在闪亮的反应表达式中使用先前的反应值?

[英]How can I use a previous reactive value in a reactive expression in shiny?

My reactive expression produces a vector of numeric values. 我的反应式表达式产生一个数值向量。 Is there a way that I can save the previous rendered values and re-use it the next time? 有没有一种方法可以保存以前的渲染值并在下次再次使用? I tried to create an additional reactive expression to save the values and then call it again when using the first reactive expression but that causes the following error: 我试图创建一个附加的反应性表达式来保存值,然后在使用第一个反应性表达式时再次调用它,但这会导致以下错误:

Error in : evaluation nested too deeply: infinite recursion / options(expressions=)?

I cannot upload my entire example since it is a survey, which is kind of confidential. 由于调查是保密的,因此我无法上传整个示例。 However, I am trying to give insights into my server.R file. 但是,我试图深入了解我的server.R文件。

yvals     <- reactive({...})

xvals     <- c(...) #some default values to start with

xvals     <- reactive({
             dat <- data.frame(xvals(), yvals())
             ....
             print(xvals)
             })

The issue is that yvals is based on the inputs of ui.R. 问题是yvals基于ui.R的输入。 However, xvals is not (at least not directly). 但是,xvals不是(至少不是直接)。 So when xvals is updating it should take the old/previous values as input. 因此,当xvals更新时,应将旧值/先前值作为输入。 I'm sorry for the mess - I'm aware that it is hard to help me without reproducible example. 我为那一团糟感到抱歉-我知道没有可复制的示例很难帮助我。 But basically, I just want to fix the previous reactive result and re-use it the next time. 但基本上,我只想修复先前的反应结果,然后在下一次重新使用它。

A bit late, but I think this is what you want - it was a good excersize. 有点晚了,但是我想这就是您想要的-这是一个很好的选择。 It uses a reactive variable memory to keep track from one iteration to the next. 它使用反应变量memory来跟踪从一个迭代到下一个迭代。 Note the isolate expression avoids the recursion error. 请注意, isolate表达式可避免递归错误。

library(shiny)

ui <- fluidPage(
  h1("Reactive Memory"),
  sidebarLayout(
    sidebarPanel(
      numericInput("val","Next Value",10)
    ),
    mainPanel(
      verbatimTextOutput("prtxval")
    )
))
server <- function(input,output,session) {

  nrowsin <- 6
  ini_xvals <- 1:nrowsin

  memory <- reactiveValues(dat = NULL)
  yvals <- reactive({ rep(input$val,nrowsin) })

  xvals <- reactive({

    isolate(dat <- memory$dat)
    if (is.null(dat)) {
      memory$dat <- data.frame(xvals = ini_xvals,yvals())
    } else {
      memory$dat <- data.frame(dat,yvals())
    }
    return(memory$dat)
  })

  output$prtxval <- renderPrint({ xvals() })
}
shinyApp(ui,server)

Picture: 图片:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM