Get startedGet started for free

The bootstrap error

You work as an R programmer for a clinical laboratory. The statistician at the lab is conducting a large scale analysis to establish ranges for the average weights of different species of mice. She has written a a function, bootstrap(), that is applied to every element of weight_list in parallel using a futures backend. However the code throws the following error:

Error in checkForRemoteErrors(val) : 
  one node produced an error: missing values and NaN's not allowed if 'na.rm' is FALSE

She cannot find the element of weight_list which causes the error. She also loses all results of the time-consuming calculation. You have been asked to fix the code. The furrr package has been loaded for you and the multisession for the futures has been planned.

This exercise is part of the course

Parallel Programming in R

View Course

Exercise instructions

  • Supply the code in the curly braces to a function that can catch the error.
  • To the error argument, supply a function that takes one argument, e, and returns the string "Error here!".
  • Map bootstrap() to all elements of the weight_list using a furrr function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

bootstrap <- function (x) {
  # Supply code to a function for catching errors
       ___({est <- rep(0, 1e3)
            for (i in 1:1e3) {
              b <- sample(x, replace = T)
              est[i] <- mean(b)
              }
    return(quantile(est, c(0.25, 0.75)))
     # Supply a function that returns a string
  }, error = ___
 )
}
# Map bootstrap() to every element of weight_list
___(___, ___)
plan(sequential)
Edit and Run Code