EVOLUTION-MANAGER
Edit File: safely.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Capture side effects.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for safely {purrr}"><tr><td>safely {purrr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Capture side effects.</h2> <h3>Description</h3> <p>These functions wrap functions so that instead of generating side effects through printed output, messages, warnings, and errors, they return enhanced output. They are all adverbs because they modify the action of a verb (a function). </p> <h3>Usage</h3> <pre> safely(.f, otherwise = NULL, quiet = TRUE) quietly(.f) possibly(.f, otherwise, quiet = TRUE) auto_browse(.f) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.f</code></td> <td> <p>A function, formula, or vector (not necessarily atomic). </p> <p>If a <strong>function</strong>, it is used as is. </p> <p>If a <strong>formula</strong>, e.g. <code>~ .x + 2</code>, it is converted to a function. There are three ways to refer to the arguments: </p> <ul> <li><p> For a single argument function, use <code>.</code> </p> </li> <li><p> For a two argument function, use <code>.x</code> and <code>.y</code> </p> </li> <li><p> For more arguments, use <code>..1</code>, <code>..2</code>, <code>..3</code> etc </p> </li></ul> <p>This syntax allows you to create very compact anonymous functions. </p> <p>If <strong>character vector</strong>, <strong>numeric vector</strong>, or <strong>list</strong>, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of <code>.default</code> will be returned.</p> </td></tr> <tr valign="top"><td><code>otherwise</code></td> <td> <p>Default value to use when an error occurs.</p> </td></tr> <tr valign="top"><td><code>quiet</code></td> <td> <p>Hide errors (<code>TRUE</code>, the default), or display them as they occur?</p> </td></tr> </table> <h3>Details</h3> <p>If you would like to include a function created with <code>safely</code>, <code>slowly</code>, or <code>insistently</code> in a package, see <a href="faq-adverbs-export.html">faq-adverbs-export</a>. </p> <h3>Value</h3> <p><code>safely</code>: wrapped function instead returns a list with components <code>result</code> and <code>error</code>. If an error occurred, <code>error</code> is an <code>error</code> object and <code>result</code> has a default value (<code>otherwise</code>). Else <code>error</code> is <code>NULL</code>. </p> <p><code>quietly</code>: wrapped function instead returns a list with components <code>result</code>, <code>output</code>, <code>messages</code> and <code>warnings</code>. </p> <p><code>possibly</code>: wrapped function uses a default value (<code>otherwise</code>) whenever an error occurs. </p> <h3>Examples</h3> <pre> safe_log <- safely(log) safe_log(10) safe_log("a") list("a", 10, 100) %>% map(safe_log) %>% transpose() # This is a bit easier to work with if you supply a default value # of the same type and use the simplify argument to transpose(): safe_log <- safely(log, otherwise = NA_real_) list("a", 10, 100) %>% map(safe_log) %>% transpose() %>% simplify_all() # To replace errors with a default value, use possibly(). list("a", 10, 100) %>% map_dbl(possibly(log, NA_real_)) # For interactive usage, auto_browse() is useful because it automatically # starts a browser() in the right place. f <- function(x) { y <- 20 if (x > 5) { stop("!") } else { x } } if (interactive()) { map(1:6, auto_browse(f)) } # It doesn't make sense to use auto_browse with primitive functions, # because they are implemented in C so there's no useful environment # for you to interact with. </pre> <hr /><div style="text-align: center;">[Package <em>purrr</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>