Skip to content

Commit 9553184

Browse files
committed
#43 - Complete code for xportr_select and associated select_log.
1 parent 5e7cd8d commit 9553184

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

R/messages.R

+20
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,23 @@ var_ord_msg <- function(reordered_vars, moved_vars, verbose) {
161161
cli_h2("All variables in dataset are ordered")
162162
}
163163
}
164+
165+
166+
#' Utility for Selecting Variables
167+
#'
168+
#' Function to output user message about variables which are listed in the
169+
#' metadata but not available in the dataframe.
170+
#'
171+
#' @param miss_vars Variables in the metadata but not in the dataframe.
172+
#' @param verbose Provides additional messaging for user.
173+
#'
174+
#' @return Output to Console
175+
#' @export
176+
select_log <- function(miss_vars, verbose) {
177+
178+
cli_h2("Variable(s) in metadata but not in dataframe.")
179+
message <- glue("Variable(s) from `metadata` not found in `.df`: ",
180+
paste0(glue("{ encode_vars(miss_vars) }"), collapse = "", sep = " "), ".")
181+
xportr_logger(message = message, type = verbose)
182+
}
183+

R/select.R

+41-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @param domain Appropriate CDISC dataset name, e.g. ADAE, DM. Used to subset the
99
#' metadata object. If none is passed, then name of the dataset passed `.df`
1010
#' 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`
1212
#' is not found in the `.df`. Options are 'stop', 'warn', 'message', and 'none'.
1313
#'
1414
#' @return Data frame containing only those variables given in the metadata.
@@ -26,27 +26,27 @@
2626
#' SEX_DEC = c("MALE", "FEMALE", "MALE")
2727
#' )
2828
#'
29-
#' metacore_adsl <- data.frame(
29+
#' metadata_adsl <- data.frame(
3030
#' dataset = "adsl",
3131
#' variable = c("USUBJID", "SITEID", "AGE", "SEX")
3232
#' )
3333
#'
34-
#' adsl <- xportr_select(adsl, metadata = metacore_adsl)
34+
#' adsl <- xportr_select(adsl, metadata = metadata_adsl)
3535
#'
3636
#' dm <- data.frame(
3737
#' USUBJID = c(1001, 1002, 1003),
3838
#' SITEID = c(001, 002, 003),
3939
#' AGE = c(63, 35, 27)
4040
#' )
4141
#'
42-
#' metacore_dm <- data.frame(
43-
#' dataset = "adsl",
42+
#' metadata_dm <- data.frame(
43+
#' dataset = "dm",
4444
#' variable = c("USUBJID", "SUBJID", "AGE", "SEX")
4545
#' )
4646
#'
47-
#' dm <- xportr_select(dm, metadata = metacore_dm, verbose = "warn")
47+
#' dm <- xportr_select(dm, metadata = metadata_dm, verbose = "warn")
4848
#'
49-
#' dm <- xportr_select(dm, metadata = metacore_dm, verbose = "stop")
49+
#' dm <- xportr_select(dm, metadata = metadata_dm, verbose = "stop")
5050

5151
xportr_select <- function(.df,
5252
metadata,
@@ -63,5 +63,39 @@ xportr_select <- function(.df,
6363
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain
6464

6565
## 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
66100
}
67101

0 commit comments

Comments
 (0)