是否可以参数化r包版本?

时间:2022-06-01 18:25:09

I'm finding myself updating a the version and date of the r-packages that I maintain quite often. Both the version and date exists in DESCRIPTION, packageName-package.Rd and also man/packageName-package.Rd. I've forgotten to update in one file several times. These were originally generated by the package.skeleton command. Is there a similar command/utility script to update the version?

我发现自己更新了我经常维护的r-packages的版本和日期。版本和日期都存在于DESCRIPTION,packageName-package.Rd以及man / packageName-package.Rd中。我忘了多次在一个文件中更新。这些最初是由package.skeleton命令生成的。是否有类似的命令/实用程序脚本来更新版本?

EDIT: Upon closer inspection the automatically generated versions and dates in the Rd files are not needed. The correct date and version still appears in the generated manual. So obviously this leave only one place to update this information.

编辑:仔细检查后,不需要在Rd文件中自动生成的版本和日期。正确的日期和版本仍会显示在生成的手册中。显然,这只留下一个地方来更新这些信息。

2 个解决方案

#1


5  

Paul Hiemstra's idea seemed very useful to me, so I wrote those few lines of codes:

Paul Hiemstra的想法对我来说似乎非常有用,所以我写了几行代码:

upVers <- function(path,update="snapshot",date=TRUE,simplify=TRUE)
{
  # This function updates the description file from package
  # in path (assumed work directory by default, as typical
  # with projects in RStudio using GitHub).

  # Usage:
    # path: path to contents of a package
    # update: What to update? "version", "major", "minor", "snapshot"
    # date: Update date as well?
    # simplfy: omit trailing zeros?

  # Assumes following numbering system:
  # version.major.minor-snapshot

  uplist <- c("version","major","minor","snapshot")

  if (missing(path)) path <- getwd()
  DESCfile <- paste0(path,"/DESCRIPTION")
  if (!file.exists(DESCfile)) stop("DESCRIPTION does not exist. Is this the folder of a package?")

  DESC <- readLines(DESCfile)

  ### Update date:
  if (date)
  {
    DESC <- gsub("(?<=Date: )\\d{4}-\\d{2}-\\d{2}",Sys.Date(),DESC,perl=TRUE)
  }

  ### Update version:
  Vers <- regmatches(DESC,regexpr("(?<=Version: )\\d+\\.?\\d*\\.?\\d*\\-?\\d*",DESC,perl=TRUE))
  Vers <- as.numeric(unlist(strsplit(Vers,split="\\.|\\-")))
  Vers <- c(Vers,rep(0,length=4-length(Vers)))
  Vers[grep(update,uplist,ignore.case=TRUE)] <- Vers[grep(update,uplist,ignore.case=TRUE)] + 1
  Vers[1:4>grep(update,uplist,ignore.case=TRUE)] <- 0

  # Combine and replace:
  Vers <- paste(paste(Vers[1:3],collapse="."),Vers[4],sep="-")
  if (simplify)
  {
    Vers <- gsub("\\.?0?\\.?0?\\-?0?$","",Vers)
  }
  DESC <- gsub("(?<=Version: )\\d+\\.?\\d*\\.?\\d*\\-?\\d*",Vers,DESC,perl=TRUE)

  # Write Description:
  writeLines(DESC,DESCfile)
}

This function updates the DESCRIPTION file using a version numbering system version.major.minor-snapshot, by default the snapshot and date are updated. For example:

此功能使用版本编号系统version.major.minor-snapshot更新DESCRIPTION文件,默认情况下更新快照和日期。例如:

# An R package:
f <- function() "foo"
package.skeleton("Foo","f")

# Update:
upVers("Foo")

# DESCIRPTION now shows version number 1.0.0-1

#2


1  

I'm not aware of such a tool, but you could leverage R functions like gsub, or external tools like grep and sed to program such behavior. This should not be more than a few lines of R.

我不知道这样的工具,但你可以利用像gsub这样的R函数,或像grep和sed这样的外部工具来编程这样的行为。这不应该超过R的几行。

#1


5  

Paul Hiemstra's idea seemed very useful to me, so I wrote those few lines of codes:

Paul Hiemstra的想法对我来说似乎非常有用,所以我写了几行代码:

upVers <- function(path,update="snapshot",date=TRUE,simplify=TRUE)
{
  # This function updates the description file from package
  # in path (assumed work directory by default, as typical
  # with projects in RStudio using GitHub).

  # Usage:
    # path: path to contents of a package
    # update: What to update? "version", "major", "minor", "snapshot"
    # date: Update date as well?
    # simplfy: omit trailing zeros?

  # Assumes following numbering system:
  # version.major.minor-snapshot

  uplist <- c("version","major","minor","snapshot")

  if (missing(path)) path <- getwd()
  DESCfile <- paste0(path,"/DESCRIPTION")
  if (!file.exists(DESCfile)) stop("DESCRIPTION does not exist. Is this the folder of a package?")

  DESC <- readLines(DESCfile)

  ### Update date:
  if (date)
  {
    DESC <- gsub("(?<=Date: )\\d{4}-\\d{2}-\\d{2}",Sys.Date(),DESC,perl=TRUE)
  }

  ### Update version:
  Vers <- regmatches(DESC,regexpr("(?<=Version: )\\d+\\.?\\d*\\.?\\d*\\-?\\d*",DESC,perl=TRUE))
  Vers <- as.numeric(unlist(strsplit(Vers,split="\\.|\\-")))
  Vers <- c(Vers,rep(0,length=4-length(Vers)))
  Vers[grep(update,uplist,ignore.case=TRUE)] <- Vers[grep(update,uplist,ignore.case=TRUE)] + 1
  Vers[1:4>grep(update,uplist,ignore.case=TRUE)] <- 0

  # Combine and replace:
  Vers <- paste(paste(Vers[1:3],collapse="."),Vers[4],sep="-")
  if (simplify)
  {
    Vers <- gsub("\\.?0?\\.?0?\\-?0?$","",Vers)
  }
  DESC <- gsub("(?<=Version: )\\d+\\.?\\d*\\.?\\d*\\-?\\d*",Vers,DESC,perl=TRUE)

  # Write Description:
  writeLines(DESC,DESCfile)
}

This function updates the DESCRIPTION file using a version numbering system version.major.minor-snapshot, by default the snapshot and date are updated. For example:

此功能使用版本编号系统version.major.minor-snapshot更新DESCRIPTION文件,默认情况下更新快照和日期。例如:

# An R package:
f <- function() "foo"
package.skeleton("Foo","f")

# Update:
upVers("Foo")

# DESCIRPTION now shows version number 1.0.0-1

#2


1  

I'm not aware of such a tool, but you could leverage R functions like gsub, or external tools like grep and sed to program such behavior. This should not be more than a few lines of R.

我不知道这样的工具,但你可以利用像gsub这样的R函数,或像grep和sed这样的外部工具来编程这样的行为。这不应该超过R的几行。