Github user adrian555 commented on a diff in the pull request:
https://github.com/apache/spark/pull/9443#discussion_r43930565
--- Diff: R/pkg/R/DataFrame.R ---
@@ -2045,3 +2045,34 @@ setMethod("attach",
}
attach(newEnv, pos = pos, name = name, warn.conflicts =
warn.conflicts)
})
+
+#' Evaluate a R expression in an environment constructed from a DataFrame
+#' with() allows access to columns of a DataFrame by simply referring to
+#' their name. It appends every column of a DataFrame into a new
+#' environment. Then, the given expression is evaluated in this new
+#' environment.
+#'
+#' @rdname with
+#' @title Evaluate a R expression in an environment constructed from a
DataFrame
+#' @param data (DataFrame) DataFrame to use for constructing an
environment.
+#' @param expr (expression) Expression to evaluate.
+#' @param ... arguments to be passed to future methods.
+#' @examples
+#' \dontrun{
+#' with(irisDf, nrow(Sepal_Width))
+#' }
+#' @seealso \link{attach}
+setMethod("with",
+ signature(data = "DataFrame"),
+ function(data, expr, ...) {
+ stopifnot(!missing(data))
+ stopifnot(inherits(data, "DataFrame"))
+ cols <- columns(data)
+ stopifnot(length(cols) > 0)
+
+ newEnv <- new.env()
+ for (i in 1:length(cols)) {
+ assign(x = cols[i], value = data[, cols[i]], envir = newEnv)
+ }
--- End diff --
Certainly I can create an internal function for this. Any rule and
suggestion on where to put and what I should name the function? Thanks.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]