Learn R Programming

abjutils (version 0.2.1)

carefully: Vectorized, parallel, safe and verbose function factory

Description

Wraps a function so that iterating over a set of inputs is easily parallelizable, and interruption-free.

Usage

carefully(.f, p = 0.05, cores = 1)

Arguments

.f

Function to be wrapped

p

Probability of function printing the index of the input it's currently processing

cores

Number of cores to use when iterating over vectorized inputs

Examples

Run this code
# NOT RUN {
# Function that takes a string and pastes two other strings
# a its beginning and end respectivelly
pad <- function(str, b = "", a = "") { paste0(b, str, a) }

# Create wrapped version of pad() that executes over 4 cores,
# captures errors, and prints its current iteration with a
# probability of 50%
new_pad <- carefully(pad, p = 0.5, cores = 4)

# Execute new_pad() with some sample data
new_pad(c("asdf", "poiu", "qwer"), b = "0", a = "1")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab