How to trigger a data refresh in shiny?
You're looking for invalidateLater
. Put this, with the appropriate interval, in the reactive expression that retrieves data from the the database.
Shiny how to refresh data loaded before server function
Using <<-
will make table
accessible globally, and after terminating your shiny app, but you need it to be reactive
. Here is a brief example on using a reactiveVal
(setting to table1
as default) that gets modified when the actionButton
is selected and a new data file is read.
library(shiny)
library(data.table)
table1 <- fread(file = 'atest1.csv')
ui <- fluidPage(
verbatimTextOutput("text"),
actionButton("refresh", "Refresh")
)
server <- function(input, output, session) {
rv <- reactiveVal(table1)
output$text <- renderText({
names(rv())
})
observeEvent(input$refresh, {
print("Refresh")
table1 <<- fread(file = 'atest2.csv')
rv(table1)
})
}
shinyApp(ui, server)
How to refresh Rdata objects in shiny app
I changed the initialization of react_data. You should get rid of the load
from your global file. This way the dataset can be garbage collected when you switch. Otherwise, it will exist in .GlobalEnv
forever.
Try this server:
shinyServer(
function(input, output, session) {
react_data <- reactiveVal()
react_data(local({load("DatasetNumber1.Rdata"); data}))
observeEvent(
input$shinyalert,
{
req(input$shinyalert)
load(paste(input$dropdown_dataset,".Rdata",sep=""))
react_data(data)
})
observeEvent(input$button_dataset, {
shinyalert(title = "Are you sure?",
text = "This action can take a while",
type = "warning",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
showCancelButton = TRUE,
showConfirmButton = TRUE,
confirmButtonText = "OK",
confirmButtonCol = "#AEDEF4",
cancelButtonText = "Cancel",
inputId = "shinyalert",
callbackR = function(x){
if(x){
showModal(modalDialog("Loading...", footer=NULL))
print(paste(input[["dropdown_dataset"]],sep=""))
removeModal()
}
}
)
})
output$test_text <- renderText(test)
output$test_plot <- renderPlot(plot(react_data()))
}
)
Related Topics
R Solve:System Is Exactly Singular
Get First and Last Values Per Group - Dplyr Group_By with Last() and First()
Plotting During a Loop in Rstudio
Calculating Time Difference Between Two Columns
Adding Empty Graphs to Facet_Wrap in Ggplot2
How to Cumulatively Add Values in One Vector in R
Efficient Calculation of Matrix Cumulative Standard Deviation in R
How to Fix 'Tar: Failed to Set Default Locale' Error
Sp::Over() for Point in Polygon Analysis
Use Expression with a Variable R
Simple Examples of Filter Function, Recursive Option Specifically