I'm trying to set up the following in shiny:
How can this be accomplished?
Based on various posts I've been trying my luck with reactivePoll
, as this seems to be most straightforward conceptually. But, depending on the exact attempt, I'm getting various problems such as checkFunc
not being found, or its input argument not being recognized.
More generally, I'm still struggling to comprehend the many ways variables can be made reactive (Advantages of reactive vs. observe vs. observeEvent) and how all of that applies to my situation.
Below some simplified code that hopefully provides the gist:
library(shiny)
library(tidyverse)
#---------create data-----------
#tibble
id=c('p1','p2','p3')
val=c(2,4,6)
my_data=tibble(id,val)
#csv
file_csv='test.csv'
write_csv(my_data,file_csv)
#my "checkFunc" routine (with input argument)
myFileCheck = function(input_path) {
if (file.exists(input_path))
file.info(input_path)$mtime[1]
else
""}
#test
tmp_time = myFileCheck(file_csv)
print(tmp_time)
#my read routine (with input argument)
myReadRoutine = function(input_path){
df = read_csv(input_path, id = 'my_source')
}
#test
tmp_df=myReadRoutine(file_csv)
print(tmp_df)
#----------shiny---------
#ui
ui = fluidPage(tableOutput('my_data'))
#server
server = function(input, output, session) {
#-----this part works---
#always read data upon launch
df_shiny=myReadRoutine(file_csv)
#some processing for display
df_shiny_display = reactive({df_shiny %>%
mutate(val2=val*10)})
#render
output$my_data = renderTable(df_shiny_display())
#-----this part doesn't----
#use custom file check function (with argument): if file has changed, use custom file reader
df_shiny = reactivePoll(1000, session, checkFunc = myFileCheck(file_csv), myReadRoutine(file_csv))
}
shinyApp(ui = ui,
server = server)
the main problem in your code is, that you assign df_shiny twice with this:
df_shiny=myReadRoutine(file_csv)
This is redundant because reactivePoll already checks every second, if the file exists and has been changed.
You have to imagine reactivePoll like an fisherman that throws his fishing rod every second. Reactive on the other hand only changes if something inside changes and then updates. So reactive is like a fridge that changes, if something inside it changes, which is why df_shiny_display changes when df_shiny is updated via reactivePull! I hope that makes sense. If you are interested when the elements trigger, you can print something to the console :)
Corrected Code:
library(shiny)
library(tidyverse)
#---------create data-----------
# Create a tibble
id <- c('p1', 'p2', 'p3')
val <- c(2, 4, 6)
my_data <- tibble(id, val)
# Write to a CSV file
file_csv <- 'test.csv'
write_csv(my_data, file_csv)
# Custom file check function
myFileCheck <- function(input_path) {
if (file.exists(input_path))
file.info(input_path)$mtime[1]
else
Sys.time() # Return current time if the file does not exist
}
# Custom read routine
myReadRoutine <- function(input_path) {
read_csv(input_path, show_col_types = FALSE)
}
#--------- Shiny App ---------
# UI
ui <- fluidPage(
tableOutput('my_data')
)
# Server
server <- function(input, output, session) {
# Monitor the file for changes using reactivePoll
df_shiny <- reactivePoll(
intervalMillis = 1000, # Check every second
session = session,
checkFunc = function() myFileCheck(file_csv), # Check if the file has changed
valueFunc = function() myReadRoutine(file_csv) # Read the file
)
# Process the data for display
df_shiny_display <- reactive({
print("updating df_shiny")
df_shiny() %>%
mutate(val2 = val * 10)
})
# Render the table
output$my_data <- renderTable({
df_shiny_display()
})
}
# Run the application
shinyApp(ui = ui, server = server)
Understanding Reactivity
Reactive
: Creates dependencies and recalculates whenever its
dependencies change. Use for values that need to be used in
downstream calculations or rendering.observe
: Executes code for its
side effects when dependencies change. Use for actions like printing, button clicking, file writing, ...observeEvent
: Similar to observe, but
triggers only when a specific event occurs.