-
Notifications
You must be signed in to change notification settings - Fork 761
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
explicit stats::setNames #734
Conversation
Does your package have a function called |
@wch but we need to fix this anyway - I think there's a bug in R CMD check where it doesn't pick up missing namespace imports for recommended packages. |
I agree that it should be fixed. I suspect there are also lots of functions from utils that are called without explicit namespacing. |
How do I fix CI? Add "stats" to Imports?
|
@hadley: nitpicking, but stats is a base package. It seems that you need to declare imports even from base packages, otherwise another package might just override a base function for you. I am not sure why it is this way, but it seems rather silly. Maybe historical reasons, or I am missing something. |
Hi @gaborcsardi I'm still getting up to speed on all this, but I think I read in Hadley's Advanced R book that only the base package itself is included in the chain of enclosing environments above a package's own namespace environment, and before the global environment, "as a convenience". All the other recommended packages are on the search path, between the global environment and the empty environment (and hence subject to the vagaries of whatever a user has installed on their search path, and in which order, in a particular session). If I understand properly, that would explain why it would be safer to |
I don't think this is true. Surely, all base and recommended packages are included there. At least you don't need to explicitly import (say) library(disposables)
pkg <- make_packages(foo = { h <- function(...) head(...) })
foo::h(1:100)
#> [1] 1 2 3 4 5 6
dispose_packages(pkg) To make things even worse, it seems attached packages are also searched, even before the recommended and base packages. Consider the following example, in which package p1 <- make_packages(foo = { density <- function(...) print("cocooo!") })
p2 <- make_packages(bar = { f <- function() density(1:10) })
bar::f()
#> [1] "cocooo!"
dispose_packages(p1)
dispose_packages(p2) Calling p1 <- make_packages(foo = { density <- function(...) print("cocooo!") })
p2 <- make_packages(bar = { f <- function() stats::density(1:10) })
bar::f()
#> Call:
#> density.default(x = 1:10)
#>
#> Data: 1:10 (10 obs.); Bandwidth 'bw' = 1.719
#>
#> x y
#> Min. :-4.1579 Min. :0.0003034
#> 1st Qu.: 0.6711 1st Qu.:0.0092711
#> Median : 5.5000 Median :0.0538486
#> Mean : 5.5000 Mean :0.0517052
#> 3rd Qu.:10.3289 3rd Qu.:0.0936045
#> Max. :15.1579 Max. :0.0997741
dispose_packages(p1)
dispose_packages(p2) If you explicitly import p1 <- make_packages(foo = { density <- function(...) print("cocooo!") })
p2 <- make_packages(bar = { f <- function() density(1:10) }, imports = "stats")
bar::f()
#> Call:
#> density.default(x = 1:10)
#>
#> Data: 1:10 (10 obs.); Bandwidth 'bw' = 1.719
#>
#> x y
#> Min. :-4.1579 Min. :0.0003034
#> 1st Qu.: 0.6711 1st Qu.:0.0092711
#> Median : 5.5000 Median :0.0538486
#> Mean : 5.5000 Mean :0.0517052
#> 3rd Qu.:10.3289 3rd Qu.:0.0936045
#> Max. :15.1579 Max. :0.0997741
dispose_packages(p1)
dispose_packages(p2) So, unless I am missing something, the best is to import everything, even stuff from base and certainly from recommended packages. But hopefully I am missing something. |
The parent environment of a package namespace is its imports environment, which contains imported objects listed in the package's NAMESPACE file. The parent of that is the base namespace, and the parent of that is the global environment. From there, you have the usual search path, of package environments for packages that are attached. You can look at this with
@Geoff99 is right that it would be safer to import objects from stats or use |
Agreed. All I am saying is that you need to do this for base packages as well (except maybe |
Hi @gaborcsardi I think we are all in violent agreement about what we need to do to be absolutely safe :-) I willingly confess I am vague about the difference between :
so I have resorted to writing myself little recursive programs using |
Could you please add a bullet point to |
Bump - let me know if you don't have time, and I can manually rebase & update news. |
I can't seem to merge this myself, so I'm just going to redo. @gaborcsardi any interesting in reporting this problem to R-devel? I think it's a problem that |
I think I deleted my fork of devtools, and now I can't access it either. Sorry about that. |
@hadley Sure, I can report it. I am pretty sure that they know about it. I am also pretty sure that they won't fix it, because it would generate NOTEs for many-many packages. It really should be fixed, though, so I'll give it a try later today. |
This has been giving me some trouble when calling
install_github
from within a package namespace.