文件不存在时如何退出downloadHandler

时间:2019-05-16 13:39:59

标签: r shiny

我有一个带有downloadButton的Shiny应用,可让用户下载一些日志文件。

由于日志文件也由logrotate处理,因此可能是在特定时间不存在日志文件,当前在尝试下载该应用程序时会中断该应用程序。

如何防止这种情况?或者如何显示modalDialog和信息,表明当前不存在任何日志文件?

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

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)

2 个答案:

答案 0 :(得分:1)

如果在没有日志文件且没有下载的情况下单击按钮,此应用程序将发出警报。

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 :(得分:1)

这是没有 bufferedReader.close(); result = stringBuilder.toString(); JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(result); JSONObject JsonResults = (JSONObject) json.get("result"); // here where the change needed if (method.equals("GET")) { String incident_state = JsonResults.get("state").toString(); // then use JSON which store results to retrieve other JSON elements by using name System.out.print(incident_state); incident.setState(Integer.parseInt(incident_state)); System.out.print("\n ServiceNow : Successful GET, incident is retrieved" ); } 的解决方案。

def save_table(sparkSession, dataframe, database, table_name, save_format="PARQUET"):
    print("Saving result in {}.{}".format(database, table_name))
    output_schema = "," \
        .join(["{} {}".format(x.name.lower(), x.dataType) for x in list(dataframe.schema)]) \
        .replace("StringType", "STRING") \
        .replace("IntegerType", "INT") \
        .replace("DateType", "DATE") \
        .replace("LongType", "INT") \
        .replace("TimestampType", "INT") \
        .replace("BooleanType", "BOOLEAN") \
        .replace("FloatType", "FLOAT")\
        .replace("DoubleType","FLOAT")
    output_schema = re.sub(r'DecimalType[(][0-9]+,[0-9]+[)]', 'FLOAT', output_schema)

    sparkSession.sql("DROP TABLE IF EXISTS {}.{}".format(database, table_name))

    query = "CREATE EXTERNAL TABLE IF NOT EXISTS {}.{} ({}) STORED AS {} LOCATION '/user/hive/{}/{}'" \
        .format(database, table_name, output_schema, save_format, database, table_name)
    sparkSession.sql(query)
    dataframe.write.insertInto('{}.{}'.format(database, table_name),overwrite = True)