Is there a neat way for functions within an R-package to get access to its version number specified in the DESCRIPTION-file (of that R-package)?
CodePudding user response:
Using installed.packages()
perhaps.
installed.packages()['MASS', 'Version']
# [1] "7.3-57"
CodePudding user response:
You can use the packageVersion()
function:
packageVersion("MASS")
#> [1] '7.3.56'
packageVersion("MASS") >= "7.3.9" # Compares as version, not character
#> [1] TRUE
Created on 2022-06-05 by the reprex package (v2.0.1)
Depending on how many packages you have installed, this will also be much faster than using installed.packages()
:
microbenchmark::microbenchmark(packageVersion("MASS"), installed.packages()['MASS', 'Version'])
#> Unit: microseconds
#> expr min lq mean median
#> packageVersion("MASS") 315.500 336.111 392.9827 370.0415
#> installed.packages()["MASS", "Version"] 4648.788 4811.913 7278.9214 4893.4105
#> uq max neval cld
#> 425.8515 796.667 100 a
#> 5055.4825 235622.901 100 b
Created on 2022-06-05 by the reprex package (v2.0.1)