-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import::from()
not working inside package function
#71
Comments
Well, that's a bummer! I hope that this was not a live error while presenting, and that it did not prevent you from submitting Not sure what the solution here is though:
|
Hah, no, nothing like that. I held off on submitting funspotr for now because was re-writing the API for the talk which I've only just finished. (Though I will want to update this part before before submitting as w/o it I'm stuck with a pesky "note".) I'll check that out, may have been a typo but yeah wouldn't think that would matter. No I don't have any expectation about colons. In I was actually noticing the issue initially when using |
Sounds like a plan! Note that |
I did not hear more about this. The |
The problem with This is the function where I'm using attach_pkg_fun <- function(pkg_fun){
pkg_nm <- pkg_fun$pkg
fun_nm <- pkg_fun$fun
pkg_nm_exp <- paste0("explicitpackage:", pkg_nm)
import::from(pkg_nm, fun_nm, .into = pkg_nm_exp, .character_only = TRUE)
}
pkg_fun <- list(pkg = "purrr", fun = "map")
attach_pkg_fun(pkg_fun)
search()
#> [1] ".GlobalEnv" "explicitpackage:purrr" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "tools:callr" "package:base" The above output is working as intended (when the function is in a standalone script) as you see However if I try calling this from a (dev version of a) package, you'll see I get an error and the search path is not edited. pkg_fun <- list(pkg = "purrr", fun = "map")
funspotr:::attach_pkg_fun(pkg_fun)
#> Error in as.environment(where): no item called "explicitpackage:purrr" on the search list
search()
#> [1] ".GlobalEnv" "package:stats" "package:graphics"
#> [4] "package:grDevices" "package:utils" "package:datasets"
#> [7] "package:methods" "Autoloads" "tools:callr"
#> [10] "package:base" This seems like some kind of environment issue that comes from it being in a package but haven't isolated it. |
OK, happy to leave this open. One question, is there a possibility import get around this using environment pointers rather than strings. As in: > x = import::into(new.env(), "%<>%", .from = magrittr)
> import::into(.GlobalEnv, "%>%", "%$%", .from = magrittr) I realize this would not solve anything directly, what I was thinking was either: a. Use base R to get a pointer to the named environment (assign it to a variable), and then use this method to import into it. |
Putting things in .GlobalEnv won't work for my use case as I need it to identify the function as coming from that package. I did try swapping |
Sorry, I was not suggesting to use the actual .GlobalEnv, just demonstrating this syntax. My question would be, does the following approach work for you: my_package_local <- new.env()
import::from(dplyr, select, .into = my_package_local)
attach(my_package_local, name = "my_package")
search()
#> [1] ".GlobalEnv" "my_package" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "tools:callr" "package:base"
ls(pos = "my_package")
#> [1] "select" Knowing the answer would help diagnose whether this is |
Yes, that works. That's the approach I've been taking brshallo/funspotr/spot_funs/L119. I'd like to get rid of I think this likely points to some edge case issue with |
OK, that definitely points to an edge case in FWIW, this is how make_attach <- attach # Make R CMD check happy.
if (use_into && !into_exists)
make_attach(NULL, 2L, name = .into) |
I'm getting the following error when running
import::from()
from the debugger after loading a personal package.Strangely I don't get any error when running that line from the debugger after doing something like
debugonce("mean"); mean(1:10)
. But I had to revert back to using the approach mentioned in #53 for attaching functions here: https://github.com/brshallo/funspotr/blob/issue-5/R/spot-funs.R#L104The text was updated successfully, but these errors were encountered: