1

I'm having a problem with recursion in my shiny app. Suppose my app reads a file and creates a dataset (dataset()). Then I have a function that processes the said dataset, creating another one (dataset.proc()). I can apply processing to the dataset as many times as I want (there are several processing methods). Another function keeps track of what's the latest dataset created (dataset.current()).

Here´s a simple example of the important part of my server.R script:

dataset <- reactive(#reads file and creates dataset)

dataset.current <- reactive ({
  if (!is.null(dataset())){
    if (!is.null(dataset.proc())) {
      return(dataset.proc())
    } else {
      return (dataset())
    }
  }
}) 

dataset.proc <- reactive(#processes dataset.current() according to user input, creates a processed dataset)

Is there any way I can 'store' dataset.proc() as a non-reactive expression to avoid the obvious error:

evaluation nested too deeply: infinite recursion

Any help would be much appreciated :)

T. Afonso
  • 13
  • 4
  • Perhaps you could store `dataset` as a reactive value (see: http://shiny.rstudio.com/reference/shiny/latest/reactiveValues.html). When changing a reactive value (i.e. loading a new data set), all other reactive functions depending on it, are triggered. – Patrick Roocks Jun 25 '16 at 13:52
  • Thank you for your anwser :) I finally solved the problem adapting the idea from the answer below. – T. Afonso Jun 25 '16 at 15:41

1 Answers1

1

You can use 'normal' R variables in the server code. For example,

current <- dataset() # Load initial dataset

shinyServer(function(input, output) {
  output$dataset.current <- renderDataTable({
    datatable(dataset.proc())
  })

  dataset.proc <- reactive({
    # Instead of dataset.current(), use the current variable

    # Processing ... 

    current <- data.frame("Processed")
    return(current)
  })
})

Before returning the result of the dataset.proc reactive expression, 'save' the result of the processing in the current variable. Next time, the dataset.proc expression can start from the value in current.

Joris Gillis
  • 136
  • 4
  • Thank you for your answer :) The problem is I need `dataset.current()` in the code, to tell me which dataset is the latest created, because there are other parts of my app that need to work with the last dataset and rely on `dataset.current()`. – T. Afonso Jun 25 '16 at 13:17
  • I just solved the problem adapting your idea. I set `current`as a global variable and then, inside de server, made it 'look' reactive to shiny so other functions could see when it changed its value with `makeReactiveBinding('current')`. Hope it helps other people too. – T. Afonso Jun 25 '16 at 15:38
  • Usually global variables could (and should) be avoided. With the answer to this question (which I did ask) I could get rid of a global variable in my Shiny app, which is a somehow similar problem (updating a data set and triggering a calculation): http://stackoverflow.com/questions/33722757/update-handsontable-by-editing-table-and-or-eventreactive – Patrick Roocks Jun 26 '16 at 13:12
  • True. A reactiveValue is indeed the betetr solution. – Joris Gillis Jun 26 '16 at 21:08