6

In R, one can figure out the version of a specific package, and use relational operators on it, with packageVersion(). For example:

packageVersion("MASS")
(pp <- packageVersion("MASS"))
## [1] ‘7.3.43’ 
pp > '7.2.0'
## TRUE

How does one get the equivalent form of version information for the running copy of R itself?

To answer this, you have to figure out exactly where to look, which is not as easy as it seems: for example

grep("R[._vV]",apropos("version"),value=TRUE)
## [1] ".ess.ESSRversion" ".ess.Rversion"    "getRversion"       
## "R_system_version"
## [5] "R.Version"        "R.version"        "R.version.string"

I'm asking this because I'm frustrated at having to figure it out every few months ... I will answer if no-one else does. Extra credit for elucidating the difference between packageVersion() and package_version() ...

I think this question is answered in passing here, but the focus of my question is specifically how to get the information in programmatic form (i.e., not just how to find out what version is running, but how to get it in a form suitable for running automated version tests within R).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    possible duplicate of [which version of R is running in my computer?](http://stackoverflow.com/questions/15983069/which-version-of-r-is-running-in-my-computer) – smci Sep 17 '15 at 22:22
  • @smci - I disagree. It's not an exact duplicate - Ben wants more than just a print out of what version is being run. – Dason Sep 18 '15 at 02:26
  • @Dason: that's not what the other question asked for, and it's not what the [answers](http://stackoverflow.com/a/15983118/202229) gave. Mine , for example. Which was IMO better than the accepted answer. Anyway: how to canonicalize questions on this topic? – smci Sep 18 '15 at 13:01
  • @smci The linked question says "now i want to know which R is running ,2.15.1 or 2.15.2" and in no way indicates that they need to programmatically compare the version numbers. – Dason Sep 18 '15 at 13:25
  • @Dason: the question also in no way says they don't want to programmatically compare them. Nor does it preclude them from just using `R --version` on a command line either. It's an open question. In my answer there I showed them how to programmatically access them. – smci Sep 19 '15 at 08:12
  • @smci I'm just saying that in my view I don't think that question is an exact duplicate. That question leaves open the possibility of them not being programmatically compared. This question wants them to be programmatically compared. I'm not trying to start an argument - just stating my opinion. And it seems like this only has one close vote (which I assume is yours?) so I can't be the only one that doesn't think it's not a duplicate. – Dason Sep 19 '15 at 18:23

2 Answers2

7

These are documented in the ?R.Version help page. It depends on exactly how you want the value formatted/stored really.

packageVersion() extracts the version info from a particular package as a package_version object.

package_version() essentially parses a version number into a package_version value that can be easily compared.

You can compare versions with

package_version(R.version) > package_version("3.0.1")

or something like that.

The getRversion() function pointed to in the ?R.Version help page automatically returns a package_version object.

getRversion() > package_version("3.0.1")

Plus the package_version objects can do automatic conversion with conformable strings as well

getRversion() > "3.0.1"
MrFlick
  • 195,160
  • 17
  • 277
  • 295
2

AFAIK, the version constant gives you that info. In my case:

version
## platform       x86_64-pc-linux-gnu         
## arch           x86_64                      
## os             linux-gnu                   
## system         x86_64, linux-gnu           
## status                                     
## major          3                           
## minor          2.2                         
## year           2015                        
## month          08                          
## day            14                          
## svn rev        69053                       
## language       R                           
## version.string R version 3.2.2 (2015-08-14)
## nickname       Fire Safety            
with(version, paste(major, minor, sep='.'))
## [1] "3.2.2"
Barranka
  • 20,547
  • 13
  • 65
  • 83