I am struggling to understand how isolate()
and reactive()
should be used in R Shiny.
I want to achieve the following:
Whenever the "Refresh" action button is clicked:
Perform a
subset
on a data.frame and,Feed this into my function to recalculate values.
The subset depends on a group of checkboxes that the user has ticked, of which there are approximately 40. I cannot have these checkboxes "fully reactive" because the function takes about 1.5 sec to execute. Instead, I want to give the user a chance to select multiple boxes and only afterwards click a button to (a) subset and (b) call the function again.
To do so, I load the data.frame in the server.R function:
df1 <- readRDS("D:/././df1.RData")
Then I have my main shinyServer function:
shinyServer(function(input, output) {
data_output <- reactive({
df1 <- df1[,df1$Students %in% input$students_selected]
#Here I want to isolate the "students_selected" so that this is only
#executed once the button is clicked
})
output$SAT <- renderTable({
myFunction(df1)
})
}
Use
eventReactive
instead:And in your UI you should have something like:
Looking at ?shiny::eventReactive:
How about something like
Here is my minimal example.
Edit
Another implementation, a bit more concise.
renderTable
by default inspects the changes in all reactive elements within the function (in this case,input$num
andinput$button
). But, you want it to react only to the button. Hence you need to put the elements to be ignored within theisolate
function. If you omit theisolate
function, then the table is updated as soon as the slider is moved.