Skip to content
Snippets Groups Projects
Commit f7f28ee7 authored by Adrian Zhuang's avatar Adrian Zhuang Committed by Shivaram Venkataraman
Browse files

[SPARK-10913] [SPARKR] attach() function support

Bring the change code up to date.

Author: Adrian Zhuang <adrian555@users.noreply.github.com>
Author: adrian555 <wzhuang@us.ibm.com>

Closes #9031 from adrian555/attach2.
parent 1e0aba90
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,7 @@ export("setJobGroup", ...@@ -23,6 +23,7 @@ export("setJobGroup",
exportClasses("DataFrame") exportClasses("DataFrame")
exportMethods("arrange", exportMethods("arrange",
"attach",
"cache", "cache",
"collect", "collect",
"columns", "columns",
......
...@@ -1881,3 +1881,33 @@ setMethod("as.data.frame", ...@@ -1881,3 +1881,33 @@ setMethod("as.data.frame",
} }
collect(x) collect(x)
}) })
#' The specified DataFrame is attached to the R search path. This means that
#' the DataFrame is searched by R when evaluating a variable, so columns in
#' the DataFrame can be accessed by simply giving their names.
#'
#' @rdname attach
#' @title Attach DataFrame to R search path
#' @param what (DataFrame) The DataFrame to attach
#' @param pos (integer) Specify position in search() where to attach.
#' @param name (character) Name to use for the attached DataFrame. Names
#' starting with package: are reserved for library.
#' @param warn.conflicts (logical) If TRUE, warnings are printed about conflicts
#' from attaching the database, unless that DataFrame contains an object
#' @examples
#' \dontrun{
#' attach(irisDf)
#' summary(Sepal_Width)
#' }
#' @seealso \link{detach}
setMethod("attach",
signature(what = "DataFrame"),
function(what, pos = 2, name = deparse(substitute(what)), warn.conflicts = TRUE) {
cols <- columns(what)
stopifnot(length(cols) > 0)
newEnv <- new.env()
for (i in 1:length(cols)) {
assign(x = cols[i], value = what[, cols[i]], envir = newEnv)
}
attach(newEnv, pos = pos, name = name, warn.conflicts = warn.conflicts)
})
...@@ -1003,3 +1003,7 @@ setGeneric("rbind", signature = "...") ...@@ -1003,3 +1003,7 @@ setGeneric("rbind", signature = "...")
#' @rdname as.data.frame #' @rdname as.data.frame
#' @export #' @export
setGeneric("as.data.frame") setGeneric("as.data.frame")
#' @rdname attach
#' @export
setGeneric("attach")
\ No newline at end of file
...@@ -1405,6 +1405,26 @@ test_that("Method as.data.frame as a synonym for collect()", { ...@@ -1405,6 +1405,26 @@ test_that("Method as.data.frame as a synonym for collect()", {
expect_equal(as.data.frame(irisDF2), collect(irisDF2)) expect_equal(as.data.frame(irisDF2), collect(irisDF2))
}) })
test_that("attach() on a DataFrame", {
df <- jsonFile(sqlContext, jsonPath)
expect_error(age)
attach(df)
expect_is(age, "DataFrame")
expected_age <- data.frame(age = c(NA, 30, 19))
expect_equal(head(age), expected_age)
stat <- summary(age)
expect_equal(collect(stat)[5, "age"], "30")
age <- age$age + 1
expect_is(age, "Column")
rm(age)
stat2 <- summary(age)
expect_equal(collect(stat2)[5, "age"], "30")
detach("df")
stat3 <- summary(df[, "age"])
expect_equal(collect(stat3)[5, "age"], "30")
expect_error(age)
})
unlink(parquetPath) unlink(parquetPath)
unlink(jsonPath) unlink(jsonPath)
unlink(jsonPathNa) unlink(jsonPathNa)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment