R 什么';在被动上下文中监视目录中的文件/文件夹数量的好方法是什么?
下面是我试图在一个闪亮的应用程序中执行的操作的简化版本:R 什么';在被动上下文中监视目录中的文件/文件夹数量的好方法是什么?,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,下面是我试图在一个闪亮的应用程序中执行的操作的简化版本: ## run a script that creates subdirectories system('Rscript /path/to/dir/dir_generating_script.R') ## observe path and count new directories created observe({ d_count <- list.dirs("/path/to/dir") %>% len
## run a script that creates subdirectories
system('Rscript /path/to/dir/dir_generating_script.R')
## observe path and count new directories created
observe({
d_count <- list.dirs("/path/to/dir") %>% length()
showNotification(paste0(
"Current dir count: ", d_count
))
})
运行创建子目录的脚本
系统('Rscript/path/to/dir/dir\u生成脚本.R')
##观察路径并计算创建的新目录数
观察({
d_计数%length()
显示通知(0)(
“当前目录计数:”,d_计数
))
})
我在这里遇到的问题是,当脚本在后台运行时,由于d_计数发生变化,通知消息没有更新。该消息仅在脚本完成运行时更新,但我希望在脚本运行时看到计数增量。是否有更好的方法以反应方式监视目录?您可以使用
reactivePoll()
创建定期检查的反应文件计数(此处为1秒):
库(闪亮)
图书馆(tidyverse)
用户界面
library(shiny)
library(tidyverse)
ui <- fluidRow(verbatimTextOutput("result"))
server <- function(input, output, session) {
file_count <- function() {
list.dirs(".", full.names = FALSE, recursive = FALSE) %>%
length()
}
current_file_count <- reactivePoll(1000, session, file_count, file_count)
output$result <- renderText(current_file_count())
}
shinyApp(ui = ui, server = server)