首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文件不存在时退出downloadHandler

如何在文件不存在时退出downloadHandler
EN

Stack Overflow用户
提问于 2019-05-16 21:39:59
回答 2查看 2.2K关注 0票数 1

我有一个闪亮的应用程序与一个downloadButton,它使用户能够下载一些日志文件。

由于日志文件也是由logrotate处理的,因此可能在某个时间不存在日志文件,这会在尝试下载应用程序时中断应用程序。

我怎样才能防止这种情况呢?或者,如何显示包含当前不存在日志文件的信息的modalDialog

我尝试包含req(F)return(FALSE),但它们都不起作用。当前的方法是有效的,因为我创建了一个空的data.frame,然后将其导出,但这不是一个很好的解决方案。

代码语言:javascript
复制
library(shiny)
library(data.table)

## Write random log file. Uncomment the next line to make the example work.
#fwrite(x = iris, file = "logs.log")


ui <- fluidPage(
  downloadButton("showLogs", label="", title="Logs herunterladen", icon = icon("book-open"))
)

server <- function(input, output, session) {
  output$showLogs <- downloadHandler(
    filename = function() {
      paste('logs-', Sys.Date(), '.csv', sep='')
    },
    content = function(file) {
      logfile <- list.files(path = ".", pattern = basename("logs.log"))
      if (length(logfile) != 0) {
        logfile <- fread(logfile, sep = ";", header = F)
        fwrite(logfile, file, sep = ";", row.names = FALSE)
      } else {
        ## Problem is in here
        # req(F)
        # return(FALSE)
        fwrite(data.frame("No log-Files"), file, sep = ";", row.names = FALSE)
      }
    }
  )
}

shinyApp(ui, server)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-17 17:17:55

这是一个没有reactiveTimer的解决方案。

代码语言:javascript
复制
library(shiny)
library(data.table)
library(shinyjs)

## Write random log file. Uncomment the next line to make the example work.
#fwrite(x = iris, file = "logs.log")

ui <- fluidPage(
  useShinyjs(),
  downloadButton("showLogs", label="", style = "display:none;"),
  actionButton("btn", "Download") 
)

server <- function(input, output, session) {

  observeEvent(input$btn, {
    logfile <- list.files(path = ".", pattern = basename("logs.log"))
    if(length(logfile)){
      runjs("$('#showLogs').click();")
    }
  })

  output$showLogs <- downloadHandler(
    filename = function() {
      paste('logs-', Sys.Date(), '.csv', sep='')
    },
    content = function(file) {
      logfile <- list.files(path = ".", pattern = basename("logs.log"))
      logfile <- fread(logfile, sep = ";", header = F)
      fwrite(logfile, file, sep = ";", row.names = FALSE)
    }
  )
}

shinyApp(ui, server)
票数 1
EN

Stack Overflow用户

发布于 2019-06-15 23:12:28

如果在日志文件不存在且无法下载的情况下单击该按钮,此应用程序会抛出警报。

代码语言:javascript
复制
library(shiny)
library(data.table)
library(shinyjs)

## Write random log file. Uncomment the next line to make the example work.
#fwrite(x = iris, file = "logs.log")

ui <- fluidPage(
  useShinyjs(),
  downloadButton("showLogs", label="", title="Logs herunterladen", icon = icon("book-open"))
)

server <- function(input, output, session) {

  autoInvalidate <- reactiveTimer(1000)

  observe({
    autoInvalidate()
    logfile <- list.files(path = ".", pattern = basename("logs.log"))
    if(length(logfile)){
      runjs("$('#showLogs').off('click.x')")
    }else{
      runjs("$('#showLogs').off('click.x').on('click.x', function(e){alert('No log file'); e.preventDefault();})")
    }
  })

  output$showLogs <- downloadHandler(
    filename = function() {
      paste('logs-', Sys.Date(), '.csv', sep='')
    },
    content = function(file) {
      logfile <- list.files(path = ".", pattern = basename("logs.log"))
      logfile <- fread(logfile, sep = ";", header = F)
      fwrite(logfile, file, sep = ";", row.names = FALSE)
    }
  )
}

shinyApp(ui, server)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56169965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档