使用 tryCatch()

我們正在定義一個功能強大的版本,它從給定的 URL 讀取 HTML 程式碼。穩健的,我們希望它處理的情況下的東西要麼出錯(錯誤)或不太我們打算給它(警告)的方式感。錯誤和警告的總稱是條件

使用 tryCatch 的功能定義

readUrl <- function(url) {
    out <- tryCatch(

        ########################################################
        # Try part: define the expression(s) you want to "try" #
        ########################################################

        {
            # Just to highlight: 
            # If you want to use more than one R expression in the "try part" 
            # then you'll have to use curly brackets. 
            # Otherwise, just write the single expression you want to try and 

            message("This is the 'try' part")
            readLines(con = url, warn = FALSE) 
        },

        ########################################################################
        # Condition handler part: define how you want conditions to be handled #
        ########################################################################

        # Handler when a warning occurs:
        warning = function(cond) {
            message(paste("Reading the URL caused a warning:", url))
            message("Here's the original warning message:")
            message(cond)

            # Choose a return value when such a type of condition occurs
            return(NULL)
        },

        # Handler when an error occurs:
        error = function(cond) {
            message(paste("This seems to be an invalid URL:", url))
            message("Here's the original error message:")
            message(cond)

            # Choose a return value when such a type of condition occurs
            return(NA)
        },

        ###############################################
        # Final part: define what should happen AFTER #
        # everything has been tried and/or handled    #
        ###############################################

        finally = {
            message(paste("Processed URL:", url))
            message("Some message at the end\n")
        }
    )    
    return(out)
}

測試一下

讓我們定義一個 URL 向量,其中一個元素不是有效的 URL

urls <- c(
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz",
    "I'm no URL"
)

並將此作為輸入傳遞給我們上面定義的函式

y <- lapply(urls, readUrl)
# Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
# Some message at the end
#
# Processed URL: http://en.wikipedia.org/wiki/Xz
# Some message at the end
#
# URL does not seem to exist: I'm no URL 
# Here's the original error message:
# cannot open the connection
# Processed URL: I'm no URL
# Some message at the end
#
# Warning message:
# In file(con, "r") : cannot open file 'I'm no URL': No such file or directory

調查輸出

length(y)
# [1] 3

head(y[[1]])
# [1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"      
# [2] "<html><head><title>R: Functions to Manipulate Connections</title>"      
# [3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
# [4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"             
# [5] "</head><body>"                                                          
# [6] ""    

y[[3]]
# [1] NA