7

How do you set the priority in which reactive() calculations are performed in Shiny? For example, there is a priority option in observe(), quoting the help file

priority
    An integer or numeric that controls the priority with which this observer 
should be executed. An observer with a given priority level will always execute
sooner than all observers with a lower priority level. Positive, negative, 
and zero values are allowed.

I wish to apply this by having my Shiny dashboard display some summary statistics before writing these statistics to a logging database, here is an example

In server.R retrieve data every hour

observe({
    invalidateLater(3600000,session)
    con <- dbConnect(PostgreSQL(), ...)

    values$data = dbGetQuery(con, "select * from table;")
})

output$text = renderText({
    temp = values$data
    print(paste("the mean is", mean(temp)))
})

observe({
    temp = values$data
    dbWriteTable(con, paste0("Insert into table_means Values (", (mean(temp),");" ))
})

And in ui.R I print the mean of the data.

I wish the rendering in the UI to occur before writing the values to my sql database

Here are some related questions:

R shiny Observe running Before loading of UI and this causes Null parameters

Community
  • 1
  • 1
Alex
  • 15,186
  • 15
  • 73
  • 127
  • 1
    Can you give a basic example of what you want to observe. Reactives are evaluated lazily. Observers are evaluated eagerly hence the need for a priority. – jdharrison Jun 03 '14 at 08:28
  • For example, I am plotting a line of best fit using `renderPlot()`, however, I recalculate the gradient of this line in an `observe()` further on so that I can write it into a database. I would like the plot to render first so that users can see it, and then write the relevant values later on in the background. – Alex Jun 03 '14 at 08:43
  • if you want the reader to see it first, don't you need a button for the user to save if desired ? – Stéphane Laurent Jun 03 '14 at 09:43
  • Thank you both for your comments. I have included a detailed example which I hope answers your question @StéphaneLaurent. – Alex Jun 03 '14 at 22:47

1 Answers1

1

I may have found the answer in this question: R shiny passing reactive to selectInput choices

**Using outputOptions one can do the following (pasted from the help file):

## Not run:
# Get the list of options for all observers within output
outputOptions(output)

# Disable suspend for output$myplot
outputOptions(output, "myplot", suspendWhenHidden = FALSE)

# Change priority for output$myplot
outputOptions(output, "myplot", priority = 10)

# Get the list of options for output$myplot
outputOptions(output, "myplot")
## End(Not run)

So this allows you to set the priority of the output items, but it still doesn't tell me how these priorities are relative to the observe() priority.

Community
  • 1
  • 1
Alex
  • 15,186
  • 15
  • 73
  • 127
  • actually I don't think this answer is correct as the responses to http://stackoverflow.com/questions/22845874/r-shiny-observe-running-before-loading-of-ui-and-this-causes-null-parameters seem to suggest that setting observer priority to a negative number does not help. – Alex Jun 03 '14 at 23:04