52
52
# '
53
53
# ' regex_named_captures(pattern=pattern_boxes, text=choices_2)
54
54
# ' }
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)
55
60
56
61
# ' @export
57
62
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
+
58
68
match <- gregexpr(pattern , text , perl = perl )[[1 ]]
59
69
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
+ ))
61
75
colnames(d ) <- capture_names
62
76
77
+
63
78
for ( column_name in colnames(d ) ) {
64
79
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
+ },
66
83
attr(match , " capture.start" )[, column_name ],
67
84
attr(match , " capture.length" )[, column_name ]
68
85
)
@@ -73,6 +90,8 @@ regex_named_captures <- function( pattern, text, perl=TRUE ) {
73
90
# ' @rdname metadata_utilities
74
91
# ' @export
75
92
checkbox_choices <- function ( select_choices ) {
93
+ checkmate :: assert_character(select_choices , any.missing = F , len = 1 , min.chars = 1 )
94
+
76
95
# The weird ranges are to avoid the pipe character; PCRE doesn't support character negation.
77
96
pattern_checkboxes <- " (?<=\\ A| \\ | )(?<id>\\ d{1,}), (?<label>[\x 21-\x 7B\x 7D-\x 7E ]{1,})(?= \\ | |\\ Z)"
78
97
0 commit comments