Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding reactive functions in Shiny

Tags:

r

rstudio

shiny

I have created a Shiny app which pulls in data from a database. I have a number of inputs in the UI part, and a number of outputs in the Server part.

In the server part I have a reactive function that builds a query using some of the inputs and then pulls in data from a database, eg:

queriedData <- reactive({
                query <- paste0(...,input$a,...);
                return(db$find(query))
               })

In the output slots, I refer to the data in using

x <- queriedData()

My questions are:

  • I believe the database is polled only when inputs that are referred to in the reactive function change. Is this correct?
  • As a consequence, am I correct in thinking that calling this reactive function does not spawn a query - ie the data is cached and the cached data is provided?
  • For inputs that are not part of the query, I am assuming changes to these do not cause a new database query. Is this correct?
like image 304
James Avatar asked Sep 25 '16 19:09

James


People also ask

What are reactive values Shiny?

Reactive values contain values (not surprisingly), which can be read by other reactive objects. The input object is a ReactiveValues object, which looks something like a list, and it contains many individual reactive values. The values in input are set by input from the web browser.

How do you make a Shiny reactive?

You can create reactive output with a two step process. Add an R object to your user interface. Tell Shiny how to build the object in the server function. The object will be reactive if the code that builds it calls a widget value.

What is reactive function?

Reactive functions are functions that can read reactive values and call other reactive functions. Whenever a reactive value changes, any reactive functions that depended on it are marked as "invalidated" and will automatically re-execute if necessary.

What does the renderPlot function do in Shiny?

renderPlot is an reactive function that can take input data from the ui. R script and feed it into the server. R script. It then actively updates the information within its function.


1 Answers

To answer your questions:

  1. Indeed, any reactive expression is only revalidated when one of the reactive expressions it depend on it is updated. Shiny does this to pass an invalidate flag whenever a value of a reactive expression changes to all other reactive expressions that depend on it. The next time these invalidated reactive expressions are used, they are recalculated. So in your case queriedData (which is a reactive expression) will be invalidated and hence updated every time it receives an invalidate flag from input$a. As the data base query is part of that calculation, your assumption is correct.
  2. That depends. When input$a didn't change and hence queriedData is not invalidated, it just returns the cached data. When input$a did change however, queriedData is recalculated and hence will spawn a query.
  3. As only reactive expressions can pass an invalidate flag, those are the only ones that can actually cause another reactive expression to be recalculated. If other parts are not reactive, they also can't invalidate and hence can't cause a recalculation of queriedData.

Keep in mind that a reactive expression doesn't necessarily need to be an input. Take following example:

query <- reactive({paste0(...,input$a,...)})
queriedData <- reactive({
                db$find(query())
               })
output$thedata <- renderDataTable(queriedData())

Now a change in input$a will invalidate query, triggering its recalculation. query, a reactive expression, will invalidate queriedData(), triggering its recalculation. This will invalidate output$thedata, and hence cause also that part to be recalculated, resulting in the new data shown in the data table.

like image 72
Joris Meys Avatar answered Oct 09 '22 23:10

Joris Meys