diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE index 2ea7486e9865..a354cdce74af 100644 --- a/R/pkg/NAMESPACE +++ b/R/pkg/NAMESPACE @@ -57,6 +57,7 @@ exportMethods( "saveAsObjectFile", "sortBy", "sortByKey", + "sumRDD", "take", "takeOrdered", "takeSample", diff --git a/R/pkg/R/RDD.R b/R/pkg/R/RDD.R index ff45a3ebc4fe..604ad03c407b 100644 --- a/R/pkg/R/RDD.R +++ b/R/pkg/R/RDD.R @@ -660,6 +660,23 @@ setMethod("minimum", reduce(x, min) }) +#' Add up the elements in an RDD. +#' +#' @param x The RDD to add up the elements in +#' @examples +#'\dontrun{ +#' sc <- sparkR.init() +#' rdd <- parallelize(sc, 1:10) +#' sumRDD(rdd) # 55 +#'} +#' @rdname sumRDD +#' @aliases sumRDD,RDD +setMethod("sumRDD", + signature(x = "RDD"), + function(x) { + reduce(x, "+") + }) + #' Applies a function to all elements in an RDD, and force evaluation. #' #' @param x The RDD to apply the function diff --git a/R/pkg/R/generics.R b/R/pkg/R/generics.R index 1e01e3601597..5fb1ccaa84ee 100644 --- a/R/pkg/R/generics.R +++ b/R/pkg/R/generics.R @@ -130,6 +130,10 @@ setGeneric("maximum", function(x) { standardGeneric("maximum") }) #' @export setGeneric("minimum", function(x) { standardGeneric("minimum") }) +#' @rdname sumRDD +#' @export +setGeneric("sumRDD", function(x) { standardGeneric("sumRDD") }) + #' @rdname name #' @export setGeneric("name", function(x) { standardGeneric("name") }) diff --git a/R/pkg/inst/tests/test_rdd.R b/R/pkg/inst/tests/test_rdd.R index e35207ac9ac4..f75e0817b940 100644 --- a/R/pkg/inst/tests/test_rdd.R +++ b/R/pkg/inst/tests/test_rdd.R @@ -286,6 +286,11 @@ test_that("minimum() on RDDs", { expect_equal(min, 1) }) +test_that("sumRDD() on RDDs", { + sum <- sumRDD(rdd) + expect_equal(sum, 55) +}) + test_that("keyBy on RDDs", { func <- function(x) { x*x } keys <- keyBy(rdd, func)