Problem
I am developing an R package and I want to increase the version automatically each time I build it. I want that to be able to associate my results to package versions. For now I was using my own ugly function to do that.
My question is: is there a way to do it better? Or, should I avoid doing that in general?
Another option
Another option I was thinking of is to install my package (hosted in github) using ´devtools::install_github´ and then save with my results (or adding to plots) the GithubSHA1 that is saved in the installed DESCRIPTION file.
For example I can get the version and GithubSHA1 like that for the ´devtools´ package:
read.dcf(file=system.file("DESCRIPTION", package="devtools"),
fields=c("Version", "GithubSHA1"))
## Version GithubSHA1
## [1,] "1.5.0.99" "3ae58a2a2232240e67b898f875b8da5e57d1b3a8"
My tries so far
I wrote the following function to produce a new DESCRIPTION file, with updated version and date. (Increasing the major version is something I don't mind increasing per hand)
incVer <- function(pkg, folder=".", increase="patch"){
## Read DESCRIPTION from installed package ´pkg´ and make new one on the specified
## ´folder´. Two options for ´increase´ are "patch" and "minor"
f <- read.dcf(file=system.file("DESCRIPTION", package=pkg),
fields=c("Package", "Type", "Title", "Version", "Date",
"Author", "Maintainer", "Description", "License",
"Depends", "Imports", "Suggests"))
curVer <- package_version(f[4])
if(increase == "patch") {
curVer[[1,3]] <- ifelse(is.na(curVer$patchlevel), 1, curVer$patchlevel + 1)
} else if (increase == "minor") {
curVer[[1,2]] <- ifelse(is.na(curVer$minor), 1, curVer$minor + 1)
curVer[[1,3]] <- 0
} else {
stop(paste("Can not identify the increase argument: " , increase))
}
f[4] <- toString(curVer)
## Update also the date
f[5] <- format (Sys.time(), "%Y-%m-%d")
write.dcf(f, file=paste(folder, "DESCRIPTION", sep="/"))
}