1#' Call function with arguments in array or data frame, returning a data frame.
2#'
3#' Call a multi-argument function with values taken from columns of an
4#' data frame or array, and combine results into a data frame
5#'
6#' @template ply
7#' @template m-
8#' @template -d
9#' @export
10#' @examples
11#' mdply(data.frame(mean = 1:5, sd = 1:5), rnorm, n = 2)
12#' mdply(expand.grid(mean = 1:5, sd = 1:5), rnorm, n = 2)
13#' mdply(cbind(mean = 1:5, sd = 1:5), rnorm, n = 5)
14#' mdply(cbind(mean = 1:5, sd = 1:5), as.data.frame(rnorm), n = 5)
15mdply <- function(.data, .fun = NULL, ..., .expand = TRUE, .progress = "none",
16                  .inform = FALSE, .parallel = FALSE, .paropts = NULL) {
17  if (is.matrix(.data) & !is.list(.data)) .data <- .matrix_to_df(.data)
18
19  f <- splat(.fun)
20  adply(.data = .data, .margins = 1, .fun = f, ...,
21    .expand = .expand, .progress = .progress, .inform = .inform,
22    .parallel = .parallel, .paropts = .paropts)
23}
24