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:
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.
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.
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.
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.
To answer your questions:
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.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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With