如何解决选择文件所在的父目录后,如何在 r Shiny 中使用reactivePoll 来检查文件是否存在
我仅在使用 reactivePoll
选择父目录后才尝试启动 shinyFiles
。我希望该功能根据选择进行检查。我不断收到关于 checkFunc
的 argument is of length 0
错误。我查了一下,通常在向量为 logical(0)
时发生。我从 reactivePoll example 得到了这个例子。请提供一些帮助,谢谢。
Warning: Error in if: argument is of length zero
reactivePoll
observeEventHandler
shiny::runApp
library(shiny)
library(shinyFiles)
home_dir <- "/top/directory"
server <- function(input,output,session) {
pdf_file <- 'full_report.pdf'
status <- reactiveValues(text = 'Idle')
shinyDirChoose(
input,"dir",roots = c(home = home_dir)
)
dir <- reactive(
basename(parseDirPath(c(home=home_dir),input$dir))
)
output$dirpath_dply <- renderText({
dir()
})
observeEvent(input$dir,{
session$sendCustomMessage('disableButton','dir')
status$text <- 'Running...'
# mimic httr post
Sys.sleep(5)
message(dir())
req(dir()) # seems to prevent error but just hangs?
omiqPDF_file <- reactivePoll(2000,session,checkFunc = function() {
if (file.exists(file.path(home_dir,dir(),pdf_file)))
file.info(file.path(home_dir,pdf_file))$mtime
else
''
},valueFunc = function() {
if ( length(list.files(path = paste0(home_dir,'/',input$dir),pattern = pdf_file,recursive=TRUE) ) > 0 )
file.info(file.path(home_dir,pdf_file))$size > 5000
else
''
}
)
observe({
if (omiqPDF_file() == TRUE) {
session$sendCustomMessage('enableButton','dir')
status$text <- paste0('Completed for ',dir())
}
})
})
output$uiStatus <- renderUI(
h4(paste0('STATUS: ',status$text),style="color:red;")
)
}
ui <- fluidPage(
singleton(tags$head(HTML('
<script type="text/javascript">
$(document).ready(function() {
// Enable button
Shiny.addCustomMessageHandler("enableButton",function(id) {
$("#" + id).removeAttr("disabled");
});
// Disable button
Shiny.addCustomMessageHandler("disableButton",function(id) {
$("#" + id).attr("disabled","true");
});
})
</script>
')
)),navbarPage(
"main",tabPanel("navbar 1",sidebarPanel(
tags$h2("HEADER"),shinyDirButton("dir","Input Directory",""),br()
),mainPanel(
h1("HEADER MAIN"),h4("DIRPATH OUTPUT"),verbatimTextOutput("dirpath_dply"),uiOutput('uiStatus')
)
),tabPanel("navbar 2","blank"),tabPanel("navbar 3","blank")
)
)
shinyApp(ui = ui,server = server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。