Permalink
Browse files

New parameter .id for rdply() to specify name of index column (defaul…

…t: .n); fixes # 142
  • Loading branch information...
1 parent cedabf7 commit 40945665266203b9766da8bc93ddba95cc205046 @krlmlr krlmlr committed with krlmlr Mar 21, 2013
Showing with 22 additions and 3 deletions.
  1. +5 −3 R/rdply.r
  2. +11 −0 inst/tests/test-list.r
  3. +6 −0 inst/tests/test-replicate.r
View
@@ -13,6 +13,8 @@
#' @param .n number of times to evaluate the expression
#' @param .expr expression to evaluate
#' @param .progress name of the progress bar to use, see \code{\link{create_progress_bar}}
+#' @param .id name of the index column, defaults to \code{".n"}. Pass
+#' \code{NULL} to avoid creation of the index column
#' @return a data frame
#' @export
#' @references Hadley Wickham (2011). The Split-Apply-Combine Strategy for
@@ -22,14 +24,14 @@
#' rdply(20, mean(runif(100)))
#' rdply(20, each(mean, var)(runif(100)))
#' rdply(20, data.frame(x = runif(2)))
-rdply <- function(.n, .expr, .progress = "none") {
+rdply <- function(.n, .expr, .progress = "none", .id = ".n") {
if (is.function(.expr)) {
f <- .expr
} else {
f <- eval.parent(substitute(function() .expr))
}
res <- rlply(.n = .n, .expr = f, .progress = .progress)
- labels <- data.frame(.n = seq_len(.n))
- list_to_dataframe(res, labels)
+ names(res) <- seq_len(.n)
+ list_to_dataframe(res, idname = .id)
}
View
@@ -19,3 +19,14 @@ test_that("list names are preserved", {
expect_that(names(llply(a)), equals(letters[1:10]))
})
+
+# Test for #142
+test_that(".n column can be renamed", {
+ f <- function() data.frame(r=runif(1))
+
+ out1 <- rdply(4, f)
+ out2 <- rdply(4, f, .id='x')
+
+ expect_equal(names(out1), c('.n', 'r'))
+ expect_equal(names(out2), c('x', 'r'))
+})
@@ -7,3 +7,9 @@ test_that("length of results are correct", {
expect_equal(length(a), 4)
expect_equal(length(b), 4)
})
+
+test_that("name of id column is set", {
+ df <- rdply(4, function() c(a=1), .id='index')
+
+ expect_equal(names(df), c('index', 'a'))
+})

0 comments on commit 4094566

Please sign in to comment.