Hadley Wickham
2010-Nov-15 23:26 UTC
[Rd] Trying to understand the search path and namespaces
Hi all, I'm trying to understand how the search path and namespaces interact. For example, take the devtools package which suggests the testthat package. Here's what the search path looks like after I load each of those packages:> library(devtools) > search()[1] ".GlobalEnv" "package:devtools" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base"> library(testthat) > search()[1] ".GlobalEnv" "package:testthat" "package:devtools" [4] "package:stats" "package:graphics" "package:grDevices" [7] "package:utils" "package:datasets" "package:methods" [10] "Autoloads" "package:base" My question is this: when I execute the test function in devtools function it calls the the test_package function in the testthat package - but that function is located higher up the search path - how does R find it? (I ask this question because I'm trying to simulate package loading from within R to simplify the development cycle, but something is missing in my knowledge of namespaces, and so I have the devel versions of my packages can't access packages that are loaded after they are) Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Henrik Bengtsson
2010-Nov-16 00:02 UTC
[Rd] Trying to understand the search path and namespaces
On Mon, Nov 15, 2010 at 3:26 PM, Hadley Wickham <hadley at rice.edu> wrote:> Hi all, > > I'm trying to understand how the search path and namespaces interact. > For example, take the devtools package which suggests the testthat > package. ?Here's what the search path looks like after I load each of > those packages: > >> library(devtools) >> search() > ?[1] ".GlobalEnv" ? ? ? ?"package:devtools" ?"package:stats" > ?[4] "package:graphics" ?"package:grDevices" "package:utils" > ?[7] "package:datasets" ?"package:methods" ? "Autoloads" > [10] "package:base" >> library(testthat) >> search() > ?[1] ".GlobalEnv" ? ? ? ?"package:testthat" ?"package:devtools" > ?[4] "package:stats" ? ? "package:graphics" ?"package:grDevices" > ?[7] "package:utils" ? ? "package:datasets" ?"package:methods" > [10] "Autoloads" ? ? ? ? "package:base" > > My question is this: when I execute the test function in devtools > function it calls the the test_package function in the testthat > package - but that function is located higher up the search path - how > does R find it?With a small risk of being incorrect (and the chance of learning something new), I'll give it a try: A search for a functions/objects/... that is not in the same package environment is done in the order that the search() path gives. The exception to this iff your package has a namespace, then the 'base' package environment is always searched first (before any other environments). The ordering of the search is important when there exist multiple package environments with the same function. Is that of any help? /Henrik> > (I ask this question because I'm trying to simulate package loading > from within R to simplify the development cycle, but something is > missing in my knowledge of namespaces, and so I have the devel > versions of my packages can't access packages that are loaded after > they are) > > Hadley > > -- > Assistant Professor / Dobelman Family Junior Chair > Department of Statistics / Rice University > http://had.co.nz/ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Duncan Murdoch
2010-Nov-16 01:43 UTC
[Rd] Trying to understand the search path and namespaces
Hadley Wickham wrote:> Hi all, > > I'm trying to understand how the search path and namespaces interact. > For example, take the devtools package which suggests the testthat > package. Here's what the search path looks like after I load each of > those packages:Luke Tierney wrote up a nice description of this a few years ago. It's either on developer.r-project.org, or in an old issue of R News. Duncan Murdoch> >> library(devtools) >> search() > [1] ".GlobalEnv" "package:devtools" "package:stats" > [4] "package:graphics" "package:grDevices" "package:utils" > [7] "package:datasets" "package:methods" "Autoloads" > [10] "package:base" >> library(testthat) >> search() > [1] ".GlobalEnv" "package:testthat" "package:devtools" > [4] "package:stats" "package:graphics" "package:grDevices" > [7] "package:utils" "package:datasets" "package:methods" > [10] "Autoloads" "package:base" > > My question is this: when I execute the test function in devtools > function it calls the the test_package function in the testthat > package - but that function is located higher up the search path - how > does R find it? > > (I ask this question because I'm trying to simulate package loading > from within R to simplify the development cycle, but something is > missing in my knowledge of namespaces, and so I have the devel > versions of my packages can't access packages that are loaded after > they are) > > Hadley >