r - 将新文件添加到响应式文件路径时,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 响应式更新以及应用程序从添加到文件夹的新文件中读取和可视化数据而无需刷新有任何想法?
不幸的是,提供可重现的示例具有挑战性,因为您需要示例的数据记录和文件创建部分才能使其工作。希望有人仍然有一些想法!谢谢!
解决方案
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])
}
)
推荐阅读
- c# - C# 在事件处理程序中获取表单引用
- c++ - 为什么自动在 C++ 中的 2D 和 1D 数组的“范围”中的工作方式不同
- stm32 - 带地址标记检测的 STM32 UART 多处理器模式。预期的数据字节被解释为地址字节
- android - 如何在 Android Studio 中使用 Mariadb/J-Connector 将所选图片上传到外部 Mariadb?
- javascript - React 空字符串显示为空格
- sql - 如何找到某个单词出现在 SQL 标题中的时间量?
- angular - Angular Reactive Form:使用 FormArray 并调整 FormControls 会导致错误路径没有表单控件元素
- python - 读取一列数据并从 csv 创建日志图
- bash - 如何从一个文件中读取 key=value 变量并在另一个文件中替换 ${key}?
- html - 如何将svg图标内联与boostrap中的文本对齐?