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

Closes #137 - Add xportr_process() to apply all and write #200

Merged
merged 32 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
63c5b7b
Add `verbose` option to metadata
EeethB Jan 8, 2024
18c766f
Add verbose to metadata
EeethB Nov 28, 2023
c96d4df
Update docs
EeethB Dec 6, 2023
f6a644b
Add basic top-level wrapper
EeethB Dec 11, 2023
21accf0
Add docs for xportr()
EeethB Jan 8, 2024
9afeeb5
Correct failing tests
EeethB Jan 9, 2024
f7cf129
Run styler
EeethB Jan 9, 2024
2d8dcb3
Merge latest from non-dynamic domain branch
EeethB Jan 10, 2024
0dc0a83
Correct typo in `xportr()` to pass test
EeethB Jan 10, 2024
88b0c5e
Document
EeethB Jan 10, 2024
226f434
Update xportr() example to use internal data
EeethB Jan 11, 2024
517c84a
Style
EeethB Jan 11, 2024
e883121
Add `xportr()` to pkgdown reference
EeethB Jan 11, 2024
1d024b6
Merge branch 'main' into 137_apply_all_write
bms63 Jan 16, 2024
f67e9c7
Merge latest metadata changes from 151
EeethB Jan 18, 2024
63beadf
Streamline documentation
EeethB Jan 20, 2024
a729e5c
Streamline documentation
EeethB Jan 20, 2024
c122df6
Adjust magrittr-conditional example
EeethB Jan 22, 2024
b4a50ca
Update metadata tests to remove unnecessary messaging
EeethB Jan 22, 2024
5a7f78b
Update `xportr()` tests to remove unnecessary messaging
EeethB Jan 22, 2024
5e723be
Update `xportr()` tests to remove unnecessary messaging
EeethB Jan 22, 2024
d0bc225
Update temp files to use `local_file()`
EeethB Jan 22, 2024
4eea166
Update temp files to use `local_file()`
EeethB Jan 22, 2024
e2f7e25
Fix unmatched paren in tests
EeethB Jan 22, 2024
1e992a9
Remove duplicate comment
EeethB Jan 22, 2024
065097f
Fix error in test file path
EeethB Jan 22, 2024
caf21da
Merge branch 'main' into 137_apply_all_write
averissimo Jan 25, 2024
9540747
Merge 151 to correct failing tests
EeethB Jan 29, 2024
5d971f2
Add `xportr()` to README
EeethB Jan 29, 2024
8e1c7fe
Merge branch 'main' into 137_apply_all_write
bms63 Jan 29, 2024
0e1a305
Add argument names to `xportr()` in README
EeethB Feb 1, 2024
8f5fc66
Run styler
EeethB Feb 1, 2024
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 @@ -5,6 +5,7 @@ export(length_log)
export(type_log)
export(var_names_log)
export(var_ord_msg)
export(xportr)
export(xportr_df_label)
export(xportr_format)
export(xportr_label)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

* Added a check for character variable lengths up to 200 bytes in `xpt_validate()`(#91, #189).

EeethB marked this conversation as resolved.
Show resolved Hide resolved
* `xportr_metadata()` can set `verbose` for a whole pipeline, i.e. setting `verbose` in `xportr_metadata()` will populate to all `xportr` functions. (#151)

* All `xportr` functions now have `verbose = NULL` as the default. If left `NULL`, the previous default of `getOption("xportr.[fn_name]_verbose")` is used (#151)

## Documentation

## Deprecation and Breaking Changes

* The `domain` argument for xportr functions will no longer be dynamically
Expand Down
8 changes: 7 additions & 1 deletion R/label.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
xportr_label <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.label_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_stop(
Expand Down Expand Up @@ -99,6 +99,12 @@ xportr_label <- function(.df,
# Check any variables missed in metadata but present in input data ---
miss_vars <- setdiff(names(.df), metadata[[variable_name]])

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.label_verbose", "none")

label_log(miss_vars, verbose)

label <- metadata[[variable_label]]
Expand Down
8 changes: 7 additions & 1 deletion R/length.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
xportr_length <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.length_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_stop(
Expand Down Expand Up @@ -106,6 +106,12 @@ xportr_length <- function(.df,
# Check any variables missed in metadata but present in input data ---
miss_vars <- setdiff(names(.df), metadata[[variable_name]])

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.length_verbose", "none")

length_log(miss_vars, verbose)

length <- metadata[[variable_length]]
Expand Down
15 changes: 9 additions & 6 deletions R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@
#' xportr_type() %>%
#' xportr_order()
#' }
xportr_metadata <- function(.df, metadata = NULL, domain = NULL) {
if (is.null(metadata) && is.null(domain)) {
stop("Must provide either metadata or domain argument")
}
## Common section to detect domain from argument or attribute
xportr_metadata <- function(.df,
metadata = NULL,
domain = NULL,
verbose = NULL) {
## Common section to detect domain from argument or pipes

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)
structure(.df,
`_xportr.df_metadata_` = metadata,
`_xportr.df_verbose_` = verbose
)
}
8 changes: 7 additions & 1 deletion R/order.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
xportr_order <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.order_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_stop(
Expand Down Expand Up @@ -120,6 +120,12 @@ xportr_order <- function(.df,
# Used in warning message for how many vars have been moved
reorder_vars <- names(df_re_ord)[names(df_re_ord) != names(.df)]

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.order_verbose", "none")

# Function is located in messages.R
var_ord_msg(reorder_vars, names(drop_vars), verbose)

Expand Down
8 changes: 7 additions & 1 deletion R/type.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
xportr_type <- function(.df,
metadata = NULL,
domain = NULL,
verbose = getOption("xportr.type_verbose", "none"),
verbose = NULL,
metacore = deprecated()) {
if (!missing(metacore)) {
lifecycle::deprecate_stop(
Expand Down Expand Up @@ -146,6 +146,12 @@ xportr_type <- function(.df,
type.y = if_else(type.y %in% numericTypes, "_numeric", type.y)
)

# Verbose should use an explicit verbose option first, then the value set in
# metadata, and finally fall back to the option value
verbose <- verbose %||%
attr(.df, "_xportr.df_verbose_") %||%
getOption("xportr.type_verbose", "none")

# It is possible that a variable exists in the table that isn't in the metadata
# it will be silently ignored here. This may happen depending on what a user
# passes and the options they choose. The check_core function is the place
Expand Down
1 change: 1 addition & 0 deletions R/xportr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#'
#'
#' @keywords internal
#' @aliases xportr-package
#'
#' @import rlang haven
#' @importFrom dplyr left_join bind_cols filter select rename rename_with n
Expand Down
75 changes: 75 additions & 0 deletions R/xportr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' Wrapper to apply all core xportr functions and write xpt
#'
#' @param .df A data frame of CDISC standard.
#' @param var_metadata A data frame containing variable level metadata
#' @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.
#' @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'
#' @param df_metadata A data frame containing dataset level metadata.
#' @param path Path where transport file will be written. File name sans will be
#' used as `xpt` name.
#' @param strict_checks If TRUE, xpt validation will report errors and not write
#' out the dataset. If FALSE, xpt validation will report warnings and continue
#' with writing out the dataset. Defaults to FALSE
averissimo marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @return Returns the input dataframe invisibly
#' @export
#'
EeethB marked this conversation as resolved.
Show resolved Hide resolved
#' @examples
#'
#' has_pkgs <- requireNamespace("admiral", quietly = TRUE) &&
#' requireNamespace("dplyr", quietly = TRUE) &&
#' requireNamespace("readxl", quietly = TRUE) &&
#' requireNamespace("rlang", quietly = TRUE)
#'
#' if (has_pkgs) {
#' adsl <- admiral::admiral_adsl
#'
#' spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr")
EeethB marked this conversation as resolved.
Show resolved Hide resolved
#'
#' var_spec <- readxl::read_xlsx(spec_path, sheet = "Variables") %>%
#' dplyr::rename(type = "Data Type") %>%
#' rlang::set_names(tolower)
#' dataset_spec <- readxl::read_xlsx(spec_path, sheet = "Datasets") %>%
#' dplyr::rename(label = "Description") %>%
#' rlang::set_names(tolower)
#'
#' adsl %>%
#' xportr_metadata(var_spec, "ADSL", verbose = "warn") %>%
#' xportr_type() %>%
#' xportr_length() %>%
#' xportr_label() %>%
#' xportr_order() %>%
#' xportr_format() %>%
#' xportr_df_label(dataset_spec) %>%
#' xportr_write("adsl.xpt")
#'
#' # `xportr()` can be used to apply a whole pipeline at once
#' xportr(adsl,
#' var_metadata = var_spec,
#' df_metadata = dataset_spec,
#' domain = "ADSL",
#' verbose = "warn",
#' path = "adsl.xpt"
#' )
#' }
EeethB marked this conversation as resolved.
Show resolved Hide resolved
xportr <- function(.df,
var_metadata = NULL,
df_metadata = NULL,
domain = NULL,
verbose = NULL,
path,
strict_checks = FALSE) {
.df %>%
xportr_metadata(var_metadata, domain = domain, verbose = verbose) %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
xportr_order() %>%
xportr_format() %>%
xportr_df_label(df_metadata) %>%
xportr_write(path)
}
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ adsl %>%

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.

```{r, message=FALSE, eval=FALSE}
EeethB marked this conversation as resolved.
Show resolved Hide resolved
```{r, warning=FALSE, message=FALSE, eval=FALSE}
adsl %>%
xportr_metadata(var_spec, "ADSL") %>%
xportr_metadata(var_spec, domain = "ADSL") %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ each function call.

``` r
adsl %>%
xportr_metadata(var_spec, "ADSL") %>%
xportr_metadata(var_spec, domain = "ADSL") %>%
xportr_type() %>%
xportr_length() %>%
xportr_label() %>%
Expand Down
6 changes: 5 additions & 1 deletion man/metadata.Rd

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

2 changes: 1 addition & 1 deletion man/xportr-package.Rd

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

83 changes: 83 additions & 0 deletions man/xportr.Rd

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

2 changes: 1 addition & 1 deletion man/xportr_label.Rd

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

2 changes: 1 addition & 1 deletion man/xportr_length.Rd

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

2 changes: 1 addition & 1 deletion man/xportr_order.Rd

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

2 changes: 1 addition & 1 deletion man/xportr_type.Rd

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

Loading
Loading