Skip to content
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

Remove dynamic domain determination #193

Merged
merged 21 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(type_log)
export(var_names_log)
export(var_ord_msg)
export(xportr_df_label)
export(xportr_domain_name)
export(xportr_format)
export(xportr_label)
export(xportr_length)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

## Deprecation and Breaking Changes

* The `domain` argument for xportr functions will no longer be dynamically
determined by the name of the data frame passed as the .df argument. This was
done to make the use of xportr functions more explicit. (#182)

* The `label` argument from the `xportr_write()` function is deprecated in favor of the `metadata` argument. (#179)

## Documentation
Expand Down
7 changes: 3 additions & 4 deletions R/df_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' label = c("Subject-Level Analysis", "Adverse Events Analysis")
#' )
#'
#' adsl <- xportr_df_label(adsl, metadata)
#' adsl <- xportr_df_label(adsl, metadata, domain = "adsl")
xportr_df_label <- function(.df,
metadata = NULL,
domain = NULL,
Expand All @@ -54,10 +54,9 @@ xportr_df_label <- function(.df,
domain_name <- getOption("xportr.df_domain_name")
label_name <- getOption("xportr.df_label")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute
averissimo marked this conversation as resolved.
Show resolved Hide resolved

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
7 changes: 3 additions & 4 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#' format = c(NA, "DATE9.")
#' )
#'
#' adsl <- xportr_format(adsl, metadata)
#' adsl <- xportr_format(adsl, metadata, domain = "adsl")
xportr_format <- function(.df,
metadata = NULL,
domain = NULL,
Expand All @@ -57,10 +57,9 @@ xportr_format <- function(.df,
format_name <- getOption("xportr.format_name")
variable_name <- getOption("xportr.variable_name")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
7 changes: 3 additions & 4 deletions R/label.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#' label = c("Unique Subject Identifier", "Study Site Identifier", "Age", "Sex")
#' )
#'
#' adsl <- xportr_label(adsl, metadata)
#' adsl <- xportr_label(adsl, metadata, domain = "adsl")
xportr_label <- function(.df,
metadata = NULL,
domain = NULL,
Expand All @@ -73,10 +73,9 @@ xportr_label <- function(.df,
variable_name <- getOption("xportr.variable_name")
variable_label <- getOption("xportr.label")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
12 changes: 6 additions & 6 deletions R/length.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#' @param metadata A data frame containing variable level metadata. See
#' 'Metadata' section for details.
#' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset
#' the metadata object. If none is passed, then name of the dataset passed as
#' .df will be used.
#' the metadata object. If none is passed, then [xportr_domain()] or
#' [xportr_metadata()] must be called before hand to set the domain as an
#' attribute of `.df`.
#' @param verbose The action this function takes when an action is taken on the
#' dataset or function validation finds an issue. See 'Messaging' section for
#' details. Options are 'stop', 'warn', 'message', and 'none'
Expand Down Expand Up @@ -62,7 +63,7 @@
#' length = c(10, 8)
elimillera marked this conversation as resolved.
Show resolved Hide resolved
#' )
#'
#' adsl <- xportr_length(adsl, metadata)
#' adsl <- xportr_length(adsl, metadata, domain = "adsl")
xportr_length <- function(.df,
metadata = NULL,
domain = NULL,
Expand All @@ -80,10 +81,9 @@ xportr_length <- function(.df,
variable_length <- getOption("xportr.length")
variable_name <- getOption("xportr.variable_name")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
25 changes: 22 additions & 3 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @return `.df` dataset with metadata and domain attributes set
#' @export
#'
#' @rdname metadata
#'
#' @examples
#'
#' metadata <- data.frame(
Expand All @@ -33,18 +35,35 @@
#' library(magrittr)
#'
#' adlb %>%
#' xportr_domain_name("adlb") %>%
#' xportr_metadata(metadata, "test") %>%
#' xportr_type() %>%
#' xportr_order()
#' }
xportr_metadata <- function(.df, metadata, domain = NULL) {
## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section

structure(.df, `_xportr.df_metadata_` = metadata)
}


#' Update Metadata Domain Name
#'
#' Similar to `xportr_metadata`, but just added the domain and not the metadata.
elimillera marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @inheritParams xportr_length
#'
#' @return `.df` dataset with domain argument set
#' @export
#'
#' @rdname metadata
xportr_domain_name <- function(.df, domain) {
attr(.df, "_xportr.df_arg_") <- domain

.df
}
7 changes: 3 additions & 4 deletions R/order.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#' order = 1:4
#' )
#'
#' adsl <- xportr_order(adsl, metadata)
#' adsl <- xportr_order(adsl, metadata, domain = "adsl")
xportr_order <- function(.df,
metadata = NULL,
domain = NULL,
Expand All @@ -76,10 +76,9 @@ xportr_order <- function(.df,
order_name <- getOption("xportr.order_name")
variable_name <- getOption("xportr.variable_name")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
1 change: 1 addition & 0 deletions R/support-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ multiple_vars_in_spec_helper2 <- function(FUN) {
local_cli_theme()

adsl %>%
xportr_domain_name("adsl") %>%
FUN(metadata) %>%
testthat::expect_no_message(message = "There are multiple specs for the same variable name")
}
5 changes: 2 additions & 3 deletions R/type.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ xportr_type <- function(.df,
numericTypes <- c(getOption("xportr.numeric_types"), "_numeric")
format_name <- getOption("xportr.format_name")

## Common section to detect domain from argument or pipes
## Common section to detect domain from argument or attribute

df_arg <- tryCatch(as_name(enexpr(.df)), error = function(err) NULL)
domain <- get_domain(.df, df_arg, domain)
domain <- get_domain(.df, domain)
bms63 marked this conversation as resolved.
Show resolved Hide resolved
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain

## End of common section
Expand Down
9 changes: 3 additions & 6 deletions R/utils-xportr.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,18 @@ xpt_validate <- function(data) {
return(err_cnd)
}

#' Get the domain from argument or from magrittr's pipe (`%>%`)
#' Get the domain from argument or from the existing domain attr
#'
#' @return A string representing the domain
#' @noRd
get_domain <- function(.df, df_arg, domain) {
get_domain <- function(.df, domain) {
elimillera marked this conversation as resolved.
Show resolved Hide resolved
if (!is.null(domain) && !is.character(domain)) {
abort(c("`domain` must be a vector with type <character>.",
x = glue("Instead, it has type <{typeof(domain)}>.")
))
}

if (identical(df_arg, ".")) {
df_arg <- get_pipe_call()
}
result <- domain %||% attr(.df, "_xportr.df_arg_") %||% df_arg
result <- domain %||% attr(.df, "_xportr.df_arg_")
result
}

Expand Down
15 changes: 8 additions & 7 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ Each `xportr_` function has been written in a way to take in a part of the speci

```{r, warning = FALSE, message=FALSE, eval=TRUE}
adsl %>%
xportr_type(var_spec, "ADSL", verbose = "warn") %>%
xportr_length(var_spec, "ADSL", verbose = "warn") %>%
xportr_label(var_spec, "ADSL", verbose = "warn") %>%
xportr_order(var_spec, "ADSL", verbose = "warn") %>%
xportr_format(var_spec, "ADSL") %>%
xportr_df_label(dataset_spec, "ADSL") %>%
xportr_write("adsl.xpt")
xportr_domain_name("ADSL") %>%
elimillera marked this conversation as resolved.
Show resolved Hide resolved
xportr_type(var_spec, verbose = "warn") %>%
xportr_length(var_spec, verbose = "warn") %>%
xportr_label(var_spec, verbose = "warn") %>%
xportr_order(var_spec, verbose = "warn") %>%
xportr_format(var_spec) %>%
xportr_df_label(dataset_spec, "ADSL") %>%
xportr_write("adsl.xpt", label = "Subject-Level Analysis Dataset")
elimillera marked this conversation as resolved.
Show resolved Hide resolved
```

The `xportr_metadata()` function can reduce duplication by setting the variable specification and domain explicitly at the top of a pipeline. If you would like to use the `verbose` argument, you will need to set in each function call.
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

<!-- badges: start -->

[<img src="https://img.shields.io/badge/Slack-RValidationHub-blue?style=flat&logo=slack">](https://RValidationHub.slack.com)
[![R build
status](https://github.com/atorus-research/xportr/workflows/R-CMD-check/badge.svg)](https://github.com/atorus-research/xportr/actions?workflow=R-CMD-check)
[<img src="https://img.shields.io/codecov/c/gh/atorus-research/xportr">](https://app.codecov.io/gh/atorus-research/xportr)
Expand Down Expand Up @@ -138,13 +137,14 @@ We have suppressed the warning for the sake of brevity.

``` r
adsl %>%
xportr_type(var_spec, "ADSL", verbose = "warn") %>%
xportr_length(var_spec, "ADSL", verbose = "warn") %>%
xportr_label(var_spec, "ADSL", verbose = "warn") %>%
xportr_order(var_spec, "ADSL", verbose = "warn") %>%
xportr_format(var_spec, "ADSL") %>%
xportr_domain_name("ADSL") %>%
xportr_type(var_spec, verbose = "warn") %>%
xportr_length(var_spec, verbose = "warn") %>%
xportr_label(var_spec, verbose = "warn") %>%
xportr_order(var_spec, verbose = "warn") %>%
xportr_format(var_spec) %>%
xportr_df_label(dataset_spec, "ADSL") %>%
xportr_write("adsl.xpt")
xportr_write("adsl.xpt", label = "Subject-Level Analysis Dataset")
```

The `xportr_metadata()` function can reduce duplication by setting the
Expand Down
13 changes: 11 additions & 2 deletions man/xportr_metadata.Rd → man/metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/xportr_df_label.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/xportr_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/xportr_label.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading