Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Only display informative error messages if .inform = T

  • Loading branch information...
commit cc36fc193ecd2a9139fbfa75d3eb4848a060124c 1 parent 4b6ba09
@hadley authored
Showing with 13 additions and 7 deletions.
  1. +1 −1  NEWS
  2. +12 −6 R/ply-list.r
View
2  NEWS
@@ -5,7 +5,7 @@ Improvements:
* all ply functions deal more elegantly when given function names: can supply a vector of function names, and name is used as label in output
* failwith and each now work with function names as well as functions (i.e. "nrow" instead of nrow)
* l*ply will use list names where present
-* error messages now give you information about where the error actual occred in your data structure - hopefully this will make problems easier to track down
+* if .inform is TRUE, error messages will give you information about where errors within your data - hopefully this will make problems easier to track down
Speed-ups
View
18 R/ply-list.r
@@ -25,13 +25,14 @@
#X
#X llply(x, mean)
#X llply(x, quantile, probs = 1:3/4)
-llply <- function(.data, .fun = NULL, ..., .progress = "none") {
+llply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE) {
pieces <- if (inherits(.data, "split")) .data else as.list(.data)
if (is.null(.fun)) return(pieces)
n <- length(pieces)
if (n == 0) return(list())
- if (is.character(.fun)) .fun <- do.call("each", as.list(.fun))
+ if (is.character(.fun)) .fun <- match.fun(.fun)
+ # .fun <- do.call("each", as.list(.fun))
if (!is.function(.fun)) stop(".fun is not a function.")
progress <- create_progress_bar(.progress)
@@ -42,10 +43,15 @@ llply <- function(.data, .fun = NULL, ..., .progress = "none") {
for(i in seq_len(n)) {
piece <- pieces[[i]]
- res <- try(.fun(piece, ...))
- if (inherits(res, "try-error")) {
- piece <- paste(capture.output(print(piece)), collapse = "\n")
- stop("with piece ", i, ": \n", piece, call. = FALSE)
+ # Display informative error messages, if desired
+ if (.inform) {
+ res <- try(.fun(piece, ...))
+ if (inherits(res, "try-error")) {
+ piece <- paste(capture.output(print(piece)), collapse = "\n")
+ stop("with piece ", i, ": \n", piece, call. = FALSE)
+ }
+ } else {
+ res <- .fun(piece, ...)
}
if (!is.null(res)) result[[i]] <- res
Please sign in to comment.
Something went wrong with that request. Please try again.