简体   繁体   English

比较R包的开发版本

[英]Comparing development versions of an R package

Short version: I'm working on an R package, and I currently have a stable version. 简短版本:我正在开发一个R包,我目前有一个稳定的版本。 I have some updates to make, and would like to compare the updated version of the code to the stable version in the same R environment. 我有一些更新,并希望将更新版本的代码与同一R环境中的稳定版本进行比较。 How do I do this? 我该怎么做呢?

I'm new to package development, and I suspect dev_mode is helpful here, but ?dev_mode hasn't helped me. 我是包装开发的新手,我怀疑dev_mode在这里很有帮助,但是?dev_mode没有帮助我。

Long version: Here's the main problem I have... Inside the package (let's call it somePackage ) I have a function 长版:这是我的主要问题...在包内(让我们称之为somePackage )我有一个功能

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

If I simply duplicate somePackage in a separate directory to make a development version, then load both, R now sees two copies of bar , which creates a conflict. 如果我只是在一个单独的目录中复制somePackage来制作开发版本,然后加载它们,R现在看到两个bar副本,这会产生冲突。 I cannot run both versions of foo and bar in the same R environment. 我不能在同一个R环境中运行两个版本的foobar

If I only had the one function, I could probably do something like somePackage::bar and somePackage_dev::bar , but in reality I have dozens of functions in somePackage and making these changes would be tedious and should be unnecessary. 如果我只有一个函数,我可能会做像somePackage::barsomePackage_dev::bar这样的东西,但实际上我在somePackage有很多函数,并且进行这些更改将是乏味的并且应该是不必要的。

The key thing here is needing to run BOTH versions of foo in the same environment so that I can quickly and easily compare the timing and output of both versions on identical inputs. 这里的关键是需要在同一环境中运行两个版本的foo ,以便我可以快速轻松地比较两个版本在相同输入上的时序和输出。

You can achieve this by loading package namespaces into environments and then attaching and detaching them. 您可以通过将包命名空间加载到环境中然后附加和分离它们来实现此目的。 You can see my answer here for more about 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 name spaces into the environments x and y without attaching them to the search path. 这会将包名称空间加载到环境xy而不会将它们附加到搜索路径。 Then you can just attach and detach to decide which version of the package you want to work with in the global environment. 然后,您可以通过attachdetach来确定要在全局环境中使用的软件包版本。

Here's a trivial example of how this would work (just imagine a is a function from your package rather than a constant): 这是一个简单的例子,说明它是如何工作的(只是想象a是你的包中的函数而不是常量):

> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM