-
-
Notifications
You must be signed in to change notification settings - Fork 3
Incorporate resolve_delayed_datasets
into resolve_delayed
#255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5146352
c7ca7c2
9ae2204
389dce6
b2b7e31
6616eab
9f2622a
e93f644
2fec9aa
97413b8
69b6de0
2c22756
cb5e366
78580ea
3308e76
a4bb6da
06c34ea
4e774d0
a068f7d
72b69ae
36a413d
e663c17
91e9351
8b02fba
adf3fc2
d914534
2493760
0b1fac4
9898e3c
0610d44
decb456
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -131,7 +131,8 @@ choices_labeled <- function(choices, labels, subset = NULL, types = NULL) { | |||||||||||
#' | ||||||||||||
#' @param data (`data.frame` or `character`) | ||||||||||||
#' If `data.frame`, then data to extract labels from. | ||||||||||||
#' If `character`, then name of the dataset to extract data from once available. | ||||||||||||
#' If `character`, then name of the dataset to extract data from once available. Keyword `"all"` | ||||||||||||
#' (default) indicates that `data` will be inherited from the `data_extract_spec` `dataname`. | ||||||||||||
#' @param subset (`character` or `function`) | ||||||||||||
#' If `character`, then a vector of column names. | ||||||||||||
#' If `function`, then this function is used to determine the possible columns (e.g. all factor columns). | ||||||||||||
|
@@ -161,7 +162,10 @@ choices_labeled <- function(choices, labels, subset = NULL, types = NULL) { | |||||||||||
#' key = default_cdisc_join_keys["ADRS", "ADRS"] | ||||||||||||
#' ) | ||||||||||||
#' | ||||||||||||
#' # delayed version | ||||||||||||
#' # delayed version with unknown dataset | ||||||||||||
#' variable_choices() | ||||||||||||
#' | ||||||||||||
#' # delayed ADRS | ||||||||||||
#' variable_choices("ADRS", subset = c("USUBJID", "STUDYID")) | ||||||||||||
#' | ||||||||||||
#' # functional subset (with delayed data) - return only factor variables | ||||||||||||
|
@@ -171,35 +175,48 @@ choices_labeled <- function(choices, labels, subset = NULL, types = NULL) { | |||||||||||
#' }) | ||||||||||||
#' @export | ||||||||||||
#' | ||||||||||||
variable_choices <- function(data, subset = NULL, fill = FALSE, key = NULL) { | ||||||||||||
variable_choices <- function(data = "all", subset = function(data) names(data), fill = FALSE, key = NULL) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
checkmate::assert( | ||||||||||||
checkmate::check_character(subset, null.ok = TRUE, any.missing = FALSE), | ||||||||||||
checkmate::check_function(subset) | ||||||||||||
checkmate::check_function(subset, args = "data") | ||||||||||||
Comment on lines
180
to
+181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since function is now default, why allow
Suggested change
Also remove |
||||||||||||
) | ||||||||||||
checkmate::assert_flag(fill) | ||||||||||||
checkmate::assert_character(key, null.ok = TRUE, any.missing = FALSE) | ||||||||||||
|
||||||||||||
UseMethod("variable_choices") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not S3? |
||||||||||||
if (is.character(data)) { | ||||||||||||
variable_choices_delayed(data = data, subset = subset, fill = fill, key = key) | ||||||||||||
} else { | ||||||||||||
variable_choices_data_frame(data = data, subset = subset, fill = fill, key = key) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
#' @rdname variable_choices | ||||||||||||
#' @export | ||||||||||||
variable_choices.character <- function(data, subset = NULL, fill = FALSE, key = NULL) { | ||||||||||||
#' @keywords internal | ||||||||||||
variable_choices_delayed <- function(data, subset = function(data) names(data), fill = FALSE, key = NULL) { | ||||||||||||
checkmate::assert_string(data) | ||||||||||||
structure(list(data = data, subset = subset, key = key), | ||||||||||||
class = c("delayed_variable_choices", "delayed_data", "choices_labeled") | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
#' @rdname variable_choices | ||||||||||||
#' @export | ||||||||||||
variable_choices.data.frame <- function(data, subset = NULL, fill = TRUE, key = NULL) { | ||||||||||||
#' @keywords internal | ||||||||||||
variable_choices_data_frame <- function(data, subset = function(data) names(data), fill = TRUE, key = NULL) { | ||||||||||||
checkmate::assert_data_frame(data, min.cols = 1) | ||||||||||||
checkmate::assert( | ||||||||||||
checkmate::check_character(subset, null.ok = TRUE), | ||||||||||||
checkmate::check_function(subset, null.ok = TRUE) | ||||||||||||
) | ||||||||||||
Comment on lines
203
to
206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These argument checks aer performed by the caller.
Suggested change
|
||||||||||||
|
||||||||||||
if (is.function(subset)) { | ||||||||||||
subset <- resolve_delayed_expr(subset, ds = data, is_value_choices = FALSE) | ||||||||||||
subset <- subset(data) | ||||||||||||
if ( | ||||||||||||
!checkmate::test_character(subset, any.missing = FALSE) || | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing values are rejected by the caller.
Suggested change
|
||||||||||||
length(subset) > ncol(data) || | ||||||||||||
anyDuplicated(subset) | ||||||||||||
) { | ||||||||||||
stop( | ||||||||||||
"variable_choices(subset) function in must return a character vector with unique", | ||||||||||||
"names from the available columns of the dataset" | ||||||||||||
) | ||||||||||||
Comment on lines
+215
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
checkmate::assert_subset(subset, c("", names(data)), empty.ok = TRUE) | ||||||||||||
|
@@ -283,8 +300,8 @@ variable_choices.data.frame <- function(data, subset = NULL, fill = TRUE, key = | |||||||||||
#' }) | ||||||||||||
#' @export | ||||||||||||
#' | ||||||||||||
value_choices <- function(data, | ||||||||||||
var_choices, | ||||||||||||
value_choices <- function(data = "all", | ||||||||||||
var_choices = variable_choices(data = data), | ||||||||||||
var_label = NULL, | ||||||||||||
subset = NULL, | ||||||||||||
sep = " - ") { | ||||||||||||
|
@@ -298,16 +315,21 @@ value_choices <- function(data, | |||||||||||
checkmate::check_function(subset) | ||||||||||||
) | ||||||||||||
checkmate::assert_string(sep) | ||||||||||||
UseMethod("value_choices") | ||||||||||||
|
||||||||||||
if (is.character(data)) { | ||||||||||||
value_choices_delayed(data = data, var_choices = var_choices, var_label = var_label, subset = subset, sep = sep) | ||||||||||||
} else { | ||||||||||||
value_choices_data_frame(data = data, var_choices = var_choices, var_label = var_label, subset = subset, sep = sep) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
#' @rdname value_choices | ||||||||||||
#' @export | ||||||||||||
value_choices.character <- function(data, | ||||||||||||
var_choices, | ||||||||||||
var_label = NULL, | ||||||||||||
subset = NULL, | ||||||||||||
sep = " - ") { | ||||||||||||
#' @keywords internal | ||||||||||||
value_choices_delayed <- function(data, | ||||||||||||
var_choices, | ||||||||||||
var_label = NULL, | ||||||||||||
subset = NULL, | ||||||||||||
sep = " - ") { | ||||||||||||
checkmate::assert_string(data, null.ok = TRUE) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
structure( | ||||||||||||
list( | ||||||||||||
data = data, | ||||||||||||
|
@@ -320,13 +342,13 @@ value_choices.character <- function(data, | |||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
#' @rdname value_choices | ||||||||||||
#' @export | ||||||||||||
value_choices.data.frame <- function(data, | ||||||||||||
#' @keywords internal | ||||||||||||
value_choices_data_frame <- function(data, | ||||||||||||
var_choices, | ||||||||||||
var_label = NULL, | ||||||||||||
subset = NULL, | ||||||||||||
sep = " - ") { | ||||||||||||
checkmate::assert_data_frame(data, min.cols = 1) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert non-zero number of rows as well? |
||||||||||||
checkmate::assert_subset(var_choices, names(data)) | ||||||||||||
checkmate::assert_subset(var_label, names(data), empty.ok = TRUE) | ||||||||||||
|
||||||||||||
|
@@ -369,7 +391,13 @@ value_choices.data.frame <- function(data, | |||||||||||
df <- unique(data.frame(choices, labels, stringsAsFactors = FALSE)) # unique combo of choices x labels | ||||||||||||
|
||||||||||||
if (is.function(subset)) { | ||||||||||||
subset <- resolve_delayed_expr(subset, ds = data, is_value_choices = TRUE) | ||||||||||||
subset <- subset(data) | ||||||||||||
if (!checkmate::test_atomic(subset) || anyDuplicated(subset)) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test this under different R versions, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could duplicates be dropped here and not raise an error? |
||||||||||||
stop( | ||||||||||||
"value_choices(subset) function must return a vector with unique values from the ", | ||||||||||||
"respective columns of the dataset." | ||||||||||||
) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
res <- choices_labeled( | ||||||||||||
choices = df$choices, | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bad idea. What if
n = 6
but there are only3
choices?And why not
slice_choices
as well?