首页 > 解决方案 > 将新文件添加到响应式文件路径时,Shiny 中的 reactiveFileReader 不会更新

问题描述

我在 R Shiny 应用程序中使用 reactiveFileReader 函数来读取和可视化实时记录的数据(1 秒数据)。数据文件存储在一个文件夹中,但每小时创建一个新文件。我正在使用一个响应式文件路径,该路径应该在创建新文件并开始读取和绘制新文件中的数据时更新,但是,reactiveFileReader 似乎对 filePath 参数的更改没有反应。该应用程序只是冻结在上一个文件的最后一个数据点,直到我刷新整个应用程序,此时新文件被可视化。

这是我用来读取数据的代码片段。

dir <- "/media/sf_D_DRIVE"      
file_name <- reactive({paste0(dir, "/", list.files(dir)[length(list.files(dir))])})
df_1 <- reactiveFileReader(intervalMillis = 100, session = session , filePath =  file_name, readFunc = read.csv)

我也尝试将 filePath 直接作为参数。

dir <- "/media/sf_D_DRIVE"      
df_1 <- reactiveFileReader(intervalMillis = 100, session = session , filePath =  paste0(dir, "/", list.files(dir)[length(list.files(dir))]), readFunc = read.csv)

是否有人对如何让 filePath 响应式更新以及应用程序从添加到文件夹的新文件中读取和可视化数据而无需刷新有任何想法?

不幸的是,提供可重现的示例具有挑战性,因为您需要示例的数据记录和文件创建部分才能使其工作。希望有人仍然有一些想法!谢谢!

标签: rshinyreactive

解决方案


reactiveFileReader是基于reactivePoll. 您可以使用reactivePoll来实现您想要的:

df_1 <- reactivePoll(
  1000, session,
  checkFunc = function(){
    # this function returns the most recent modification time in the folder
    files <- list.files(dir, full.names = TRUE)
    info <- file.info(files)
    max(info$mtime)
  },
  valueFunc = function(){
    # this function returns the content of the most recent file in the folder
    files <- list.files(dir, full.names = TRUE)
    info <- file.info(files)
    i <- which.max(info$mtime)
    read.csv(files[i])
  }
)

推荐阅读