Skip to content

Commit f52259d

Browse files
authored
Merge pull request #3 from bedapub/devel
Addressing suggestions raised by CRAN reviewers
2 parents 240886a + 9329162 commit f52259d

27 files changed

Lines changed: 126 additions & 160 deletions

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
^bin$
1919
^CRAN-SUBMISSION$
2020
^CRAN-PREPARATION\.md$
21+
^CRAN-FEEDBACK\.md$
22+
cran-fixes.patch

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ symbols.rds
77
*.dll
88
.Rhistory
99
.Rproj.user
10+
cran-fixes.patch
11+
*.tar.gz
12+
ribiosArg.Rcheck

DESCRIPTION

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ Authors@R:
99
role = c("aut", "cre", "ctb"),
1010
email = "jitao_david.zhang@roche.com",
1111
comment = c(ORCID="0000-0002-3085-0909")),
12-
person("F.Hoffmann-La Roche AG", role="cph"))
12+
person(given = "Balazs",
13+
family = "Banfai",
14+
role = "ctb"))
1315
Description: Provides functions to handle command-line arguments for R
1416
scripting. It enables building stand-alone R programs that accept and
15-
parse command-line options in BIOS style.
17+
parse command-line options in 'BIOS' style.
18+
Zhang (2025) <https://github.com/bedapub/ribiosArg>.
1619
Depends:
1720
R (>= 3.4.0),
1821
ribiosUtils

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(argGet)
44
export(argGetPos)
5+
export(argIsInit)
56
export(argParse)
67
export(argPresent)
78
export(existArg)

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2026-02-12: v1.5-1
2+
+ Add R-level guards in argPresent, argGet, and argGetPos to return safe defaults when the parser is uninitialized
3+
+ Export argIsInit() for users to check parser initialization status
4+
+ argParse returns silently instead of stopping when no script context is detected (fixes --run-donttest examples)
5+
16
2015-09-04: v1.1-18
27
+ rarg_parse: fixed a bug caused by passing SEXP as a size_t object
38

NEWS.md

Lines changed: 0 additions & 101 deletions
This file was deleted.

R/argparse.R

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#' Parser of command-line parameters in BIOS style
2-
#' @aliases argIsInit
32
#' @aliases argPresent
43
#'
54
#' @param optargs String describing optional arguments. Syntax: \code{<optname1>[,paramcnt1] <optname2>[,paramcnt2]\dots}. Example: \dQuote{verbose outfile,1} means the command line has the syntax \code{prog [-verbose] [outfile name]}. It can be an empty string to express \dQuote{no options}. The value for \code{paramcnt} is 0.
@@ -37,7 +36,7 @@
3736
#' @useDynLib ribiosArg, .registration=TRUE, .fixes="C_"
3837
#'
3938
#' @examples
40-
#' \dontrun{
39+
#' \donttest{
4140
#' argParse("verbose threshold,2", "infile outfile",
4241
#' usage="prog [-infile ]infile [-outfile ]outfile [-verbose] [-threshold MIN MAX]")
4342
#' argIsInit()
@@ -70,8 +69,8 @@ argParse <- function(optargs, reqargs, usage=paste(scriptName(), "-h"), strict=T
7069
efind <- which(isE)
7170
allComm <- allComm[-(1:(efind+1))]
7271
} else {
73-
stop("This should not happen: no parameters in the form of '-f' or '--f' is detected. Please contact the developer")
74-
}
72+
return(invisible(NULL))
73+
}
7574
comm <- allComm[!grepl("^--", allComm)]
7675
## the following code was valid till R-3.0.x.
7776
## if("--args" %in% allComm) {
@@ -114,6 +113,10 @@ argParse <- function(optargs, reqargs, usage=paste(scriptName(), "-h"), strict=T
114113
}
115114
}
116115

116+
#' Check whether the argument parser has been initialized
117+
#'
118+
#' @return Logical, \code{TRUE} if \code{argParse} has been called, \code{FALSE} otherwise.
119+
#' @export
117120
argIsInit <- function() .Call(C_rarg_isInit)
118121

119122
#' Test whether the given option is present in the command line or not
@@ -127,6 +130,7 @@ argPresent <- function(opt) {
127130
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. FALSE is returned")
128131
return(FALSE)
129132
}
133+
if(!isTRUE(argIsInit())) return(FALSE)
130134
.Call(C_rarg_present, opt)
131135
}
132136

@@ -147,14 +151,15 @@ argPresent <- function(opt) {
147151
#' @seealso \code{\link{argParse}}, \code{\link{argGet}}, and \code{\link{argPresent}}
148152
#'
149153
#' @examples
150-
#' \dontrun{argGetPos("thresholds", ind=2)}
154+
#' \donttest{argGetPos("thresholds", ind=2)}
151155
#'
152156
#' @export
153157
argGetPos <- function(opt, ind=1L, default=NULL, choices=NULL) {
154158
if(isDebugging()) {
155159
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. Default value is returned")
156160
return(default)
157-
}
161+
}
162+
if(!isTRUE(argIsInit())) return(default)
158163
if(argPresent(opt)) {
159164
res <- .Call(C_rarg_getPos, opt,as.integer(ind))
160165
if(!is.null(choices) && !res %in% choices)
@@ -182,14 +187,15 @@ argGetPos <- function(opt, ind=1L, default=NULL, choices=NULL) {
182187
#' @seealso \code{\link{argParse}}, \code{\link{argGetPos}}, and \code{\link{argPresent}}
183188
#'
184189
#' @examples
185-
#' \dontrun{argGet("infile")}
190+
#' \donttest{argGet("infile")}
186191
#'
187192
#' @export
188193
argGet <- function(opt, default=NULL, choices=NULL) {
189194
if(isDebugging()) {
190195
message("[DEBUGGIING] The script is running in an interactive session, e.g. debugging mode. Default value is returned")
191196
return(default)
192197
}
198+
if(!isTRUE(argIsInit())) return(default)
193199
if(argPresent(opt)) {
194200
res <- .Call(C_rarg_get, opt)
195201
if(!is.null(choices) && !res %in% choices) {

R/parseFuncs.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @param failVal If the parsing failed (for example length not correct, or non-numeric values were provided, this value will be returned
1414
#' @param sep Separator in the character string, default ","
1515
#'
16+
#' @return A numeric vector of the parsed values, or \code{failVal} if parsing fails.
1617
#' @seealso \code{\link{argGet}}
1718
#'
1819
#' @export
@@ -127,6 +128,7 @@ parsePairs <- function(str, collapse=",", sep="=",
127128
#' @param make.names Should names be converted to adhere to the rule of variable names in R
128129
#' @param verbose Logical vector
129130
#'
131+
#' @return A factor with the specified levels.
130132
#' @export
131133
#' @examples
132134
#' makeFactor(c("A", "B", "C", "C", "A"), levels=LETTERS[3:1])
@@ -178,6 +180,7 @@ makeFactor <- function(groups, levels=NULL, make.names=TRUE, verbose=FALSE) {
178180
#' @param make.names Logical, should names be converted to adhere to the rule of variable names in R
179181
#' @param collapse Character used in \code{relevels} to collapse different levels
180182
#'
183+
#' @return A factor parsed from the input string with the specified levels.
181184
#' @export
182185
#' @examples
183186
#' parseFactor("A,B,C,B,A", rlevels="A,B,C")
@@ -194,14 +197,14 @@ makeFactor <- function(groups, levels=NULL, make.names=TRUE, verbose=FALSE) {
194197
#' groups <- factor(c("B", "C", "A", "D"), levels=c("D","C","A","B"))
195198
#' makeFactor(groups)
196199
#'
197-
#' \dontrun{
200+
#' \donttest{
198201
#' groups <- c("ATest", "Control", "Control", "ATest")
199202
#' levels <- c("Control", "ATest", "Unknown")
200203
#' makeFactor(groups, levels)
201204
#'
202205
#' groups <- c("ATest", "Control", "Control", "ATest", "BTest")
203206
#' levels <- c("Control", "ATest")
204-
#' makeFactor(groups, levels)
207+
#' try(makeFactor(groups, levels))
205208
#' }
206209
#'
207210
parseFactor <- function(str, rlevels=NULL, make.names=TRUE, collapse=",") { ## CL=command line
@@ -233,6 +236,7 @@ isDir <- function(str) file.info(str)$isdir
233236
#' @param recursive In cse of directory or compressed files, whether files should be found recursively
234237
#' @param ignore.case In case of directory or compressed files, whether case should be ignored
235238
#'
239+
#' @return A character vector of file paths.
236240
#' @importFrom ribiosUtils extname
237241
#' @importFrom utils untar unzip
238242
#' @export

R/ribiosArg.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#' Command-line argument handling for R scripting
2-
#' @docType package
3-
#' @description Provides command-line argument handling for R scripting
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
#' ribiosIO
5+
#' ribiosIO provides Command-line argument handling for R scripting
46
#' @author Jitao David Zhang <jitao_david.zhang@roche.com>
57
#' @useDynLib ribiosArg, .registration=TRUE, .fixes="C_"
6-
#' @name ribiosArg-package
8+
#' @name ribiosArg
79
NULL

R/scriptInit.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#' Prepare the environment for a script
2-
#' @aliases initScript
3-
#'
2+
#'
43
#' This function is called at the beginning of an Rscript, in order to
54
#' prepare the R environment to run in a script setting.
65
#'
7-
#' @return Only side effect is used
8-
#'
6+
#' @return No return value, called for side effects.
7+
#'
8+
#' @aliases initScript
99
#' @export
1010
#' @examples
11-
#' \dontrun{
11+
#' \donttest{
1212
#' scriptInit()
1313
#' }
1414
scriptInit <- function() {
15+
old <- options()
16+
on.exit(options(old))
1517
if(interactive()) {
1618
setDebug()
1719
} else {

0 commit comments

Comments
 (0)