8
8
# ' @param domain Appropriate CDISC dataset name, e.g. ADAE, DM. Used to subset the
9
9
# ' metadata object. If none is passed, then name of the dataset passed `.df`
10
10
# ' will be used.
11
- # ' @param verbose The action the function takes when a variable in the `metacore `
11
+ # ' @param verbose The action the function takes when a variable in the `metadata `
12
12
# ' is not found in the `.df`. Options are 'stop', 'warn', 'message', and 'none'.
13
13
# '
14
14
# ' @return Data frame containing only those variables given in the metadata.
26
26
# ' SEX_DEC = c("MALE", "FEMALE", "MALE")
27
27
# ' )
28
28
# '
29
- # ' metacore_adsl <- data.frame(
29
+ # ' metadata_adsl <- data.frame(
30
30
# ' dataset = "adsl",
31
31
# ' variable = c("USUBJID", "SITEID", "AGE", "SEX")
32
32
# ' )
33
33
# '
34
- # ' adsl <- xportr_select(adsl, metadata = metacore_adsl )
34
+ # ' adsl <- xportr_select(adsl, metadata = metadata_adsl )
35
35
# '
36
36
# ' dm <- data.frame(
37
37
# ' USUBJID = c(1001, 1002, 1003),
38
38
# ' SITEID = c(001, 002, 003),
39
39
# ' AGE = c(63, 35, 27)
40
40
# ' )
41
41
# '
42
- # ' metacore_dm <- data.frame(
43
- # ' dataset = "adsl ",
42
+ # ' metadata_dm <- data.frame(
43
+ # ' dataset = "dm ",
44
44
# ' variable = c("USUBJID", "SUBJID", "AGE", "SEX")
45
45
# ' )
46
46
# '
47
- # ' dm <- xportr_select(dm, metadata = metacore_dm , verbose = "warn")
47
+ # ' dm <- xportr_select(dm, metadata = metadata_dm , verbose = "warn")
48
48
# '
49
- # ' dm <- xportr_select(dm, metadata = metacore_dm , verbose = "stop")
49
+ # ' dm <- xportr_select(dm, metadata = metadata_dm , verbose = "stop")
50
50
51
51
xportr_select <- function (.df ,
52
52
metadata ,
@@ -63,5 +63,39 @@ xportr_select <- function(.df,
63
63
if (! is.null(domain )) attr(.df , " _xportr.df_arg_" ) <- domain
64
64
65
65
# # End of common section
66
+
67
+ if (inherits(metadata , " Metacore" )) {
68
+ metadata <- metadata $ var_spec
69
+ }
70
+
71
+ if (domain_name %in% names(metadata )) {
72
+ metadata <- metadata %> %
73
+ dplyr :: filter(!! sym(domain_name ) == domain )
74
+ }
75
+
76
+ dfvars <- names(.df )
77
+ metavars <- metadata [[variable_name ]]
78
+
79
+ if (all(metavars %in% dfvars ) == FALSE ) {
80
+ miss_vars <- metavars [which(! metavars %in% dfvars )]
81
+ select_log(miss_vars , verbose )
82
+ cat(" \n " )
83
+ }
84
+
85
+ drop_vars <- dfvars [which(! dfvars %in% metavars )]
86
+ if (length(drop_vars ) > 0 ) {
87
+ .df <- .df %> %
88
+ select(- all_of(drop_vars ))
89
+
90
+ cli_alert_info(" The following variable(s) have been dropped from `.df`:" )
91
+ # cat(paste0(drop_vars, collapse = "\n"))
92
+ cli_text(" Variables: {drop_vars}." )
93
+ cat(" \n " )
94
+ } else {
95
+ cli_alert_info(" No variables have been dropped from `.df`." )
96
+ cat(" \n " )
97
+ }
98
+
99
+ .df
66
100
}
67
101
0 commit comments