paleolimbot commented on code in PR #13960:
URL: https://github.com/apache/arrow/pull/13960#discussion_r956373824
##########
r/R/dplyr-eval.R:
##########
@@ -121,3 +128,120 @@ format_expr <- function(x) {
}
head(out, 1)
}
+
+# vector of function names that do not have corresponding bindings, but we
+# shouldn't try to translate
+translation_exceptions <- c(
+ "c",
+ "$",
+ "factor",
+ # "~",
+ # "(",
+ "across",
+ ":",
+ "[",
+ "regex",
+ "fixed",
+ "list",
+ "%>%",
+ # all the types functions
+ "int8",
+ "int16",
+ "int32",
+ "int64",
+ "uint8",
+ "uint16",
+ "uint32",
+ "uint64",
+ "float16",
+ "halffloat",
+ "float32",
+ "float",
+ "float64",
+ "boolean",
+ "bool",
+ "utf8",
+ "large_utf8",
+ "binary",
+ "large_binary",
+ "fixed_size_binary",
+ "string",
+ "date32",
+ "date64",
+ "time32",
+ "time64",
+ "duration",
+ "null",
+ "timestamp",
+ "decimal",
+ "decimal128",
+ "decimal256"
+)
+
+register_user_bindings <- function(quo, .env) {
+ unknown_functions_chr <- setdiff(
+ all_funs(quo),
+ union(
+ names(.env),
+ translation_exceptions
+ )
+ )
+
+ if (length(unknown_functions_chr != 0)) {
+ # get the actual functions from the quosure's original environment or, if
+ # the call contains `::`, get the function from the namespace
+ unknown_functions <- purrr::map_if(
+ .x = unknown_functions_chr,
+ .p = ~ !grepl("::", .x),
+ .f = ~ tryCatch(as_function(.x, env = rlang::quo_get_env(quo)), error =
function(e) NULL),
+ .else = ~ asNamespace(sub(":{+}.*?$", "", .x))[[sub("^.*?:{+}", "", .x)]]
+ )
+
+ # set the original quosure environment as the parent environment for the
+ # functions
+ parent.env(.env) <- rlang::quo_get_env(quo)
Review Comment:
I don't think so: the `environment(the_function_you_return)` definitely
definitely has to inherit from `environment(the_function_the_user_passed_in)`.
The first step here is to give `.env` a better name, because usually `.env`
is used to pass down some calling environment and the fact that I can't tell
what it is from reading your code means that future you and me are also going
to wonder.
I'm not familiar with the terminology "contains" for environments...I'm 99%
sure that the chain of environments has to be:
```r
the_old_function_environment <- environment(unknown_fn)
copy_of_nse_funcs <- as.environment(as.list(nse_funcs))
parent.env(copy_of_nse_funcs) <- the_old_function_environment
environment(unknown_fn) <- copy_of_nse_funcs
```
That may be what you're doing, but if it is, the current implementation
doesn't communicate that!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]