Using reactivePoll to monitor and process file changes

I’m trying to set up the following in shiny:

  1. read source file using a custom read in routine
  2. process dataframe and render
  3. monitor source file for changes using custom file change checker: when source file has changed, repeat 1 and 2

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật