@@ -47,17 +47,35 @@ vdat_inspect <- function(vdata_file, ...) {
4747
4848 # ## Split according to colons followed by multiple spaces.
4949 metadata <- metadata | >
50- strsplit(" :\\ s+" ) | >
51- # ## Variables with multiple entries have those entries indented and split
52- # ## into different lines. Split these into two columns according to a
53- # ## space that precedes an alphanumeric character
50+ strsplit(" :(\\ s+|$)" ) | >
51+ # ## Variables with multiple entries either have those entries indented and
52+ # ## split into different lines OR split into different lines and preceeded
53+ # ## with a dash. Split these into two columns according to a space that
54+ # ## precedes an alphanumeric character OR a dash that precedes a space.
5455 lapply(function (. ) {
5556 unlist(
56- strsplit(. , " \\ s{2,}(?=[[:alpha:]])" , perl = TRUE )
57+ strsplit(
58+ . ,
59+ " (\\ s{2,}(?=[[:alpha:]]))|-\\ s|\\ s(?=(\\ [|\\ ())" ,
60+ perl = TRUE
61+ )
5762 )
63+ })
64+
65+ # ## Now that every section has at least two entries, bind them together
66+ metadata <- do.call(rbind , metadata ) | >
67+ # silence warning associated with rbind auto-filling while letting
68+ # others through
69+ withCallingHandlers(warning = function (w ) {
70+ if (
71+ grepl(
72+ " number of columns of result is not a multiple of vector length" ,
73+ conditionMessage(w )
74+ )
75+ ) {
76+ invokeRestart(" muffleWarning" )
77+ }
5878 }) | >
59- # ## Now that every section has two entries, bind them together
60- do.call(rbind , args = _) | >
6179 data.frame () | >
6280 # ## Remove section headers
6381 _[- section_header_indices , ]
@@ -71,17 +89,35 @@ vdat_inspect <- function(vdata_file, ...) {
7189 x [which(isnotblank )][cumsum(isnotblank )]
7290 }
7391
74- metadata $ X1 <- locf(metadata $ X1 )
92+ # ## Rename
93+ names(metadata ) <- c(" variable" , " subvariable" , " value" )
94+
95+ metadata $ variable <- locf(metadata $ variable )
7596
7697 # ## Add back section headers
7798 metadata $ section <- section_headers
7899
79100 # ## Remove redundant variables
80- metadata <- metadata [metadata $ X1 != metadata $ X2 , ]
81-
82- # ## Rename
83- names(metadata ) <- c(" variable" , " value" , " section" )
101+ metadata <- metadata [metadata $ variable != metadata $ subvariable , ]
84102 rownames(metadata ) <- NULL
85103
104+ # ## Reorganize values
105+ metadata $ value <- ifelse(
106+ metadata $ variable == metadata $ value ,
107+ metadata $ subvariable ,
108+ metadata $ value
109+ )
110+ metadata $ subvariable <- ifelse(
111+ metadata $ subvariable == metadata $ value ,
112+ NA ,
113+ metadata $ subvariable
114+ )
115+
116+ # ## Convert NA in value column
117+ metadata [metadata $ value == " N/A" , " value" ] <- NA
118+
119+ # ## Drop parentheses
120+ metadata $ value <- gsub(" \\ ]|\\ [|\\ (|\\ )" , " " , metadata $ value )
121+
86122 invisible (metadata )
87123}
0 commit comments