diff --git a/NAMESPACE b/NAMESPACE index d2f10378..ca12c8c1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/process.R b/R/process.R deleted file mode 100644 index 0dd16c95..00000000 --- a/R/process.R +++ /dev/null @@ -1,17 +0,0 @@ -xportr_process <- function(.df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.type_verbose", "none"), - path, - strict_checks = FALSE - ) { - .df %>% - xportr_metadata(metadata, domain) %>% - xportr_type() %>% - xportr_length() %>% - xportr_label() %>% - xportr_order() %>% - xportr_format() %>% - xportr_df_label(dataset_spec) %>% - xportr_write("adsl.xpt") -} diff --git a/R/xportr.R b/R/xportr.R new file mode 100644 index 00000000..687e66d6 --- /dev/null +++ b/R/xportr.R @@ -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 +#' +#' @return Returns the input dataframe invisibly +#' @export +#' +#' @examples +#' +#' has_pkgs <- require(admiral, quietly = TRUE) && +#' require(dplyr, quietly = TRUE) && +#' require(readxl, quietly = TRUE) && +#' require(rlang, quietly = TRUE) +#' +#' if (has_pkgs) { +#' adsl <- admiral::admiral_adsl +#' +#' spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr") +#' +#' 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" +#' ) +#' } +xportr <- function(.df, + var_metadata = NULL, + df_metadata = NULL, + domain = NULL, + verbose = NULL, + path, + strict_checks = FALSE) { + .df %>% + xportr_metadata(var_metadata, domain, verbose) %>% + xportr_type() %>% + xportr_length() %>% + xportr_label() %>% + xportr_order() %>% + xportr_format() %>% + xportr_df_label(dataset_spec) %>% + xportr_write(path) +} diff --git a/man/xportr.Rd b/man/xportr.Rd new file mode 100644 index 00000000..4a391256 --- /dev/null +++ b/man/xportr.Rd @@ -0,0 +1,83 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/xportr.R +\name{xportr} +\alias{xportr} +\title{Wrapper to apply all core xportr functions and write xpt} +\usage{ +xportr( + .df, + var_metadata = NULL, + df_metadata = NULL, + domain = NULL, + verbose = NULL, + path, + strict_checks = FALSE +) +} +\arguments{ +\item{.df}{A data frame of CDISC standard.} + +\item{var_metadata}{A data frame containing variable level metadata} + +\item{df_metadata}{A data frame containing dataset level metadata.} + +\item{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.} + +\item{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'} + +\item{path}{Path where transport file will be written. File name sans will be +used as \code{xpt} name.} + +\item{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} +} +\value{ +Returns the input dataframe invisibly +} +\description{ +Wrapper to apply all core xportr functions and write xpt +} +\examples{ + +has_pkgs <- require(admiral, quietly = TRUE) && + require(dplyr, quietly = TRUE) && + require(readxl, quietly = TRUE) && + require(rlang, quietly = TRUE) + +if (has_pkgs) { + adsl <- admiral::admiral_adsl + + spec_path <- system.file(paste0("specs/", "ADaM_admiral_spec.xlsx"), package = "xportr") + + 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" + ) +} +}