Comparison of R package development versions

Short version: I am working on an R package and I currently have a stable version. I have some updates that need to be done and would like to compare the updated version of the code with the stable version in the same R environment. How do I do this?

I'm new to package development and I suspect dev_mode is useful here, but dev_mode didn't help me.

Long version: Here is the main problem I have ... Inside the package (let me call it somePackage

) I have a function

foo <- function(x){
    y <- bar(x) # some other function defined inside my package
    y
}

      

If I just duplicate somePackage

in a separate directory to make a development version download both, R now sees two copies bar

, which creates a conflict. I cannot run both versions foo

and bar

in the same R environment.

If I only had one function, I could probably do something like somePackage::bar

and somePackage_dev::bar

, but I actually have dozens of functions in somePackage

and making these changes would be tedious and shouldn't be unnecessary.

The key point here is the need to run BOTH versions foo

in the same environment so that I can quickly and easily compare the time and output of both versions on the same inputs.

+3


source to share


1 answer


You can achieve this by loading package namespaces into environments and then adding and detaching them. You can see my answer here for more information on this strategy. But basically, you can do as you say and then load the namespace for both versions with something like:

x <- loadNamespace('somePackage')
y <- loadNamespace('somePackage_dev')

      

This loads the package namespaces into the environments x

and y

without binding them to the search path. Then you can simply attach

and detach

decide which version of the package you want to use in the global environment.



Here's a trivial example of how this would work (just imagine a

- a function from your package, not a constant):

> x <- new.env()
> y <- new.env()
> search()
 [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
 [4] "package:grDevices" "package:utils"     "package:datasets" 
 [7] "package:devtools"  "package:methods"   "Autoloads"        
[10] "package:base"     
> x$a <- 1
> y$a <- 2
> a
Error: object 'a' not found
> x$a
[1] 1
> y$a
[1] 2
> attach(x)
> search()
 [1] ".GlobalEnv"        "x"                 "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:devtools"  "package:methods"  
[10] "Autoloads"         "package:base"
> a
[1] 1
> detach(x)
> a
Error: object 'a' not found
> attach(y)
> search()
 [1] ".GlobalEnv"        "y"                 "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:devtools"  "package:methods"  
[10] "Autoloads"         "package:base" 
> a
[1] 2
> detach(y)
> a
Error: object 'a' not found

      

+4


source







All Articles