Managing File Read Failures

Let’s say we have a collection of file paths, and we want to use the readLines function to read the contents of each file into a list. We want to use tryCatch to manage the error and return an error message when one of the files does not exist. Here is an illustration of how to do it: 

R




# establish a list of file paths
file_paths <- c("file1.txt", "file2.txt",
                "file3.txt")
  
# create a function to read a file's data
read_file_contents <- function(file_path) {
  tryCatch({
    return(readLines(file_path))
  }, error = function(e) {
    return(paste("Error: Unable to read file",
                 file_path))
  })
}
  
# using lapply, read each file's text into 
# a list, and using tryCatch to deal with errors.
file_contents <- lapply(file_paths,
                        read_file_contents)
  
# outcome the file contents
print(file_contents)


Output:

[[1]]
[1] "Error: Unable to read file file1.txt"

[[2]]
[1] "Error: Unable to read file file2.txt"

[[3]]
[1] "Error: Unable to read file file3.txt"

In the given example, “the read_file_contents” function first tries to use “readLines(file_path)” to read the contents of the input file. If there are any problems, the “tryCatch” function’s “error” section is called and returns an error message.

TryCatch with parLapply (Parallel package) in R

R has a useful function called ‘tryCatch’ that enables you to handle errors and warnings that could happen while your code is being executed. When combined with the parallel package function ‘parLapply’, which parallelly applies a function to each member of a list, you can run many instances of your code at once and manage any problems or warnings that may come up as each iteration is being executed.

  • R’s ‘tryCatch’ function enables you to detect and deal with problems and warnings that could appear while your code is being executed. The function requires two arguments: the expression to be evaluated and a list of handlers outlining what should be done in the event of an error or warning.
  • The R Programming Language function ‘parLapply’ applies a function in parallel to each member of a list using the parallel package. The list to be processed, the function to apply to each element of the list, and the number of cores to be used for parallel processing are the three arguments required by the function.

Here is an illustration of how to process a list of inputs in parallel using tryCatch and parLapply.

R




library(parallel)
  
# To handle errors and warnings, write
# a function containing tryCatch clauses.
my_function <- function(x) {
  tryCatch({
    # Code that needs to be run for each list element
    result <- sqrt(x)
    return(result)
  }, error = function(e) {
    # code that will be executed in the event of an error
    return(NA)
  }, warning = function(w) {
    #code that will be executed in the event of a warning
    return(NA)
  })
}
  
# Build a cluster object.
cl <- makeCluster(2)
  
# Make a list of the inputs that
# will be handled concurrently.
inputs <- list(1, 2, 3, 4, 5)
  
# Apply the function in parallel to
# each entry of the list.
results <- parLapply(cl, inputs, my_function)
  
# Put an end to the cluster
stopCluster(cl)
  
# Print the outcomes
print(results)


Output:

1
1.414214
1.732051
2
2.236068
  • The function  ‘my_function’ in this example is defined with ‘tryCatch’ statements to deal with errors and warnings. Then, we use ‘parLapply’  to parallelize the application of the function ‘my_function’ to each element of the list of inputs to be processed.
  • The cluster object ‘cl ‘ produced by the ‘makeCluster()’ method is supplied as the first argument to the ‘parLapply()’ function in this version. The ‘stopCluster()’ function is used to stop the cluster after processing is finished.
  • The’ barplot()’ function is included towards the conclusion. The nested list of results is transformed into a flat vector and provided to barplot using the ‘unlist()’  method.(). Xlab, Ylab, and main are used to label the axes and the title of the chart, while the ‘names.arg’  argument is used to label the bars with the inputs.

Let’s say we want to use the ‘parLapply’ function to compute the median for each vector in a collection of numeric vectors. One of the vectors does, however, contain a character value that the median algorithm is unable to handle. ‘TryCatch’ will be used to handle the error, and ‘NA’ will be returned for the associated output list element. Here is an illustration of how to do it. Another instance where the code’s error section executes is as follows:

R




library(parallel)
  
# make a cluster with 4 nodes.
cl <- makeCluster(4)
  
# create a function to determine a
# numeric vector's median
compute_median <- function(x) {
  tryCatch({
    return(median(x))
  }, error = function(e) {
    return(NA)
  })
}
  
# create a collection of numerical vectors 
# for which the median should be calculated.
x_list <- list(c(11, 12, 13), 
               c(14, 15, 16), 
               c("a", "b", "c"), 
               c(17, 18, 19))
  
# Using parLapply, compute the medians in
# parallel, and tryCatch to manage errors 
medians <- parLapply(cl, x_list, compute_median)
  
# shut the cluster
stopCluster(cl)
  
# print the medians
print(medians)


Output:

[[1]]
[1] 12

[[2]]
[1] 15

[[3]]
[1] "b"

[[4]]
[1] 18

In this example, the first and second elements of the input list were processed successfully and returned the expected median values of 12 and 15, respectively. The third element of the input list resulted in an error due to the presence of character values, and the ‘error’ part of the ‘tryCatch’ function was executed, returning ‘NA’ for that element. Finally, the fourth element of the input list was processed successfully and returned the expected median value of 18.

Similar Reads

Handling Missing Numbers

...

Managing File Read Failures

...

Correcting a Division-by-Zero Mistake

Let’s say we want to substitute the missing values in each column of a data frame with the mean value for that column. TryCatch will be used to manage any potential errors, and an error message will be returned. Here is an illustration of how to do it:...

Conclusion

...