Skip to content

Instantly share code, notes, and snippets.

@paulrougieux
Forked from anonymous/warningerrorlog.R
Last active July 12, 2017 08:49

Revisions

  1. paulrougieux revised this gist Jul 12, 2017. 1 changed file with 38 additions and 5 deletions.
    43 changes: 38 additions & 5 deletions warningerrorlog.R
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,41 @@
    #' Download a file
    #' Test error with
    #' downloadfile2("xxxx")
    # Load the 2 functions below,
    # then test errors with:
    # downloadfile1(url="xxxx", destfile="/tmp/xxxx", logfile="~/rlog.txt")
    # ~/rlog.txt contains
    # Error in download.file(url = url, destfile = destfile, ...): cannot download all files
    #
    # then
    #
    # downloadfile2(url="xxxx", destfile="/tmp/xxxx", logfile="~/rlog.txt")
    # ~/rlog.txt contains the warning message
    # simpleWarning in download.file(url = url, destfile = destfile, ...): URL 'HTTP://xxxx/': status was 'Couldn't resolve host name'
    # but it doesn't contain the error message, how to capture both error and warning?


    #' Download a file, catch errors only
    #' @param url an url
    #' @param destfile, destination file
    downloadfile2 <- function(url, destfile,...){
    #' @param logfile, log file
    #' @param ... other parameters passed to \link{download.file}
    downloadfile1 <- function(url, destfile, logfile, ...){
    downloadstatus <- 1
    tryCatch({
    # Store the download status returned by download.file
    downloadstatus <- download.file(url = url, destfile = destfile, ...)

    }, error = function(errorcondition){
    # Add error message to the log file
    write(toString(errorcondition), logfile, append=TRUE)
    }
    )
    # Returns the download status
    invisible(downloadstatus)
    }



    #' Download a file, catch errors and warnings
    downloadfile2 <- function(url, destfile, logfile, ...){
    downloadstatus <- 1
    tryCatch({
    # Store the download status returned by download.file
    @@ -19,4 +51,5 @@ downloadfile2 <- function(url, destfile,...){
    )
    # Returns the download status
    invisible(downloadstatus)
    }
    }

  2. @invalid-email-address Anonymous created this gist Jul 12, 2017.
    22 changes: 22 additions & 0 deletions warningerrorlog.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #' Download a file
    #' Test error with
    #' downloadfile2("xxxx")
    #' @param url an url
    #' @param destfile, destination file
    downloadfile2 <- function(url, destfile,...){
    downloadstatus <- 1
    tryCatch({
    # Store the download status returned by download.file
    downloadstatus <- download.file(url = url, destfile = destfile, ...)

    }, error = function(errorcondition){
    # Add error message to the log file
    write(toString(errorcondition), logfile, append=TRUE)
    }, warning = function(warningcondition){
    # Add warning message to the log file
    write(toString(warningcondition), logfile, append=TRUE)
    }
    )
    # Returns the download status
    invisible(downloadstatus)
    }