[Rd] Problem understanding behaviour of versionCheck for loadNamespace (and when versions for Imports packages are checked)

Duncan Murdoch murdoch.duncan at gmail.com
Thu Nov 27 01:43:17 CET 2014


On 26/11/2014, 5:39 PM, Geoff Lee wrote:
> Hi
> 
>  
> 
> I'm still exploring the R programming universe, so if this is being asked in
> the wrong place, or in the wrong way (e.g. too verbose or lacking in crucial
> detail or in the wrong format) please let me know
> 
>  
> 
> I am trying to understand when the version constraints for packages which
> appear in the Imports field of a DESCRIPTION file are checked.
> 
>  
> 
> Along the way I've hit a snag understanding what loadNamespace with the
> versionCheck argument set does.  The core of my query is 'am I doing
> something wrong, or have I stumbled across a bug?'.  Probably the former,
> but after several days I still can't figure it out, so any guidance, hints
> or outright help would be much appreciated.
> 
>  
> 
> Thanks in advance
> 
>  
> 
> Geoff
> 
>  
> 
> What I've tried so far.
> 
>  
> 
> "Writing R extensions" section 1.1.3 says 
> 
>  
> 
> "The 'Imports' field lists packages whose namespaces are imported from (as
> specified in the NAMESPACE file) but which do not need to be attached.  ...
> shortened for brevity ... Packages declared in the 'Depends' field should
> not also be in the 'Imports' field. Version requirements can be specified
> and are checked when the namespace is loaded (since R >= 3.0.0). "
> 
>  
> 
> It is slightly ambiguous, but seems to mean that the version dependencies
> for both Depends packages and Imports packages are checked when the package
> (namespace?) is loaded.
> 
>  
> 
> The release notes for R3.0.0 are more direct.  They say
> 
>  
> 
> "loadNamespace() allows a version specification to be given, and this is
> used to check version specifications given in the Imports field when a
> namespace is loaded"
> 
>  
> 
> But some toy (locally built and loaded) examples seem to show that while the
> Depends versions are checked, the Imports version constraints are not (on
> Windows 64, running R3.1.2, see full session_info later).
> 
>  
> 
> My tests (package2 imports package1) use implicit loading (via the
> package1::fun1() idiom) so I have worked back to try get a minimal example
> of what's causing me problems. I have tried
> 
>  
> 
> loadNamespace('package2', versionCheck = list (op = ">=", version =
> package_version('3.0')))

You need to look at how versionCheck is used in the existing code.  It
is used on items that were produced when the package was installed, and
in those, the length of the versionCheck entry really is 3.  For
example, the dplyr package imports "lazyeval (>= 0.1.8)".  That
specification becomes the list

 ..$ lazyeval  :List of 3
  .. ..$ name   : chr "lazyeval"
  .. ..$ op     : chr ">="
  .. ..$ version:Classes 'package_version', 'numeric_version'  hidden
list of 1
  .. .. ..$ : int [1:3] 0 1 8

So if you want to put one of these together, your versionCheck list
should be list(name = "package2", op = ">=", version =
package+version('3.0'))

Duncan Murdoch

> 
>  
> 
> This should fail (package2 has version 0.3, not 3.0) but instead it seems to
> load package2, version 0.3 OK.
> 
>  
> 
> Reading the code of loadNamespace, there is some code which says
> 
>  
> 
> if (length(z <- versionCheck) == 3L && !do.call(z$op, 
> 
>     list(as.numeric_version(version), z$version))) 
> 
>     stop(gettextf("namespace %s %s is being loaded, but %s %s is required", 
> 
>       sQuote(package), version, z$op, z$version), 
> 
>        domain = NA)
> 
>  
> 
> I think it is the length(z <- versionCheck) == 3L part of the if test that
> is allowing the incorrect version be loaded.
> 
>  
> 
> The documentation for loadNamespace says that "versionCheck" is
> 
> "NULL or a version specification (a list with components op and version))."
> 
>  
> 
> If I add a third (nonsense) component to the versionCheck argument list,
> then loadNamespace does what I expect.  Is there supposed to be a third
> component in the list, and if so what should it be? Or is this a bug?
> 
>  
> 
> I've got this far and am now stumped, hence my query.
> 
>  
> 
> Some output from my tests.
> 
>  
> 
> d> devtools::session_info()
> 
> Session
> info--------------------------------------------------------------------
> 
>   setting  value                       
> 
> version  R version 3.1.2 (2014-10-31)
> 
> system   x86_64, mingw32             
> 
> ui       RStudio (0.98.953)          
> 
> language (EN)                        
> 
> collate  English_Australia.1252      
> 
> tz       Australia/Sydney            
> 
>  
> 
> Packages--------------------------------------------------------------------
> ----
> 
>   package    * version date       source        
> 
> devtools     1.6.1   2014-10-07 CRAN (R 3.1.2)
> 
> rstudioapi   0.1     2014-03-27 CRAN (R 3.1.1)
> 
>  
> 
> d> # I think this should fail but it does not
> 
> d> loadNamespace('package2', versionCheck = list(op='>=', version =
> package_version('3.0')))
> 
> <environment: namespace:package2>
> 
>   
> 
>   
> 
>   d> devtools::session_info()
> 
> Session
> info--------------------------------------------------------------------
> 
>   setting  value                       
> 
> version  R version 3.1.2 (2014-10-31)
> 
> system   x86_64, mingw32             
> 
> ui       RStudio (0.98.953)          
> 
> language (EN)                        
> 
> collate  English_Australia.1252      
> 
> tz       Australia/Sydney            
> 
>  
> 
> Packages--------------------------------------------------------------------
> ----
> 
>   package    * version date       source        
> 
> devtools     1.6.1   2014-10-07 CRAN (R 3.1.2)
> 
> package2     0.3     2014-11-24 local         
> 
> rstudioapi   0.1     2014-03-27 CRAN (R 3.1.1)
> 
>  
> 
> d> #Try again with a third (nonsense) entry in the versionCheck list
> 
> d> unloadNamespace('package2')
> 
> d> loadNamespace('package2', versionCheck = list(op='>=', version =
> package_version('3.0'), please = 'check it'))
> 
> Error in loadNamespace("package2", versionCheck = list(op = ">=", version =
> package_version("3.0"),  : 
> 
>  namespace 'package2' 0.3 is being loaded, but >= 3.0 is required
> 
>  
> 
>  
> 
>  
> 
> d> devtools::session_info()
> 
> Session
> info--------------------------------------------------------------------
> 
> setting  value                       
> 
> version  R version 3.1.2 (2014-10-31)
> 
> system   x86_64, mingw32             
> 
> ui       RStudio (0.98.953)          
> 
> language (EN)                        
> 
> collate  English_Australia.1252      
> 
> tz       Australia/Sydney            
> 
>  
> 
> Packages--------------------------------------------------------------------
> ----
> 
> package    * version date       source        
> 
> devtools     1.6.1   2014-10-07 CRAN (R 3.1.2)
> 
> rstudioapi   0.1     2014-03-27 CRAN (R 3.1.1)
> 
> d> loadNamespace('package2', versionCheck = list(op='>=', version =
> package_version('0.3'), please = 'check it'))
> 
> <environment: namespace:package2>
> 
> d>
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list