Skip to content

Commit c8bf54d

Browse files
committed
check parameters of metadata utilities
ref #190
1 parent 175f700 commit c8bf54d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

R/metadata-utilities.R

+21-2
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,34 @@
5252
#'
5353
#' regex_named_captures(pattern=pattern_boxes, text=choices_2)
5454
#' }
55+
#'
56+
#' path_3 <- system.file(package="REDCapR", "test-data/project-simple/simple-metadata.csv")
57+
#' ds_metadata_3 <- read.csv(path_3, stringsAsFactors=FALSE)
58+
#' choices_3 <- ds_metadata_3[ds_metadata_3$field_name=="race", "select_choices_or_calculations"]
59+
#' regex_named_captures(pattern=pattern_boxes, text=choices_3)
5560

5661
#' @export
5762
regex_named_captures <- function( pattern, text, perl=TRUE ) {
63+
64+
checkmate::assert_character(pattern, any.missing=F, min.chars=0L, len=1)
65+
checkmate::assert_character(text , any.missing=F, min.chars=0L, len=1)
66+
checkmate::assert_logical( perl , any.missing=F)
67+
5868
match <- gregexpr(pattern, text, perl=perl)[[1]]
5969
capture_names <- attr(match, "capture.names")
60-
d <- as.data.frame(matrix(NA, nrow=length(attr(match, "match.length")), ncol=length(capture_names)))
70+
d <- as.data.frame(matrix(
71+
data = NA_character_,
72+
nrow = length(attr(match, "match.length")),
73+
ncol = length(capture_names)
74+
))
6175
colnames(d) <- capture_names
6276

77+
6378
for( column_name in colnames(d) ) {
6479
d[[column_name]] <- mapply(
65-
function (start, len) substr(text, start, start+len-1L),
80+
function (start, len) {
81+
substr(text, start, start+len-1L)
82+
},
6683
attr(match, "capture.start" )[, column_name],
6784
attr(match, "capture.length")[, column_name]
6885
)
@@ -73,6 +90,8 @@ regex_named_captures <- function( pattern, text, perl=TRUE ) {
7390
#' @rdname metadata_utilities
7491
#' @export
7592
checkbox_choices <- function( select_choices ) {
93+
checkmate::assert_character(select_choices, any.missing=F, len=1, min.chars=1)
94+
7695
#The weird ranges are to avoid the pipe character; PCRE doesn't support character negation.
7796
pattern_checkboxes <- "(?<=\\A| \\| )(?<id>\\d{1,}), (?<label>[\x21-\x7B\x7D-\x7E ]{1,})(?= \\| |\\Z)"
7897

man/metadata_utilities.Rd

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)