Skip to content

Commit

Permalink
fix order variable. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
elimillera committed Apr 27, 2022
1 parent 53fa5a0 commit 766e5c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ importFrom(glue,glue_collapse)
importFrom(graphics,stem)
importFrom(janitor,make_clean_names)
importFrom(magrittr,"%>%")
importFrom(magrittr,extract2)
importFrom(purrr,map)
importFrom(purrr,map2_chr)
importFrom(purrr,map_chr)
Expand All @@ -52,6 +53,7 @@ importFrom(stringr,str_extract)
importFrom(stringr,str_replace)
importFrom(stringr,str_replace_all)
importFrom(tidyselect,all_of)
importFrom(tidyselect,any_of)
importFrom(tm,stemDocument)
importFrom(utils,capture.output)
importFrom(utils,packageVersion)
Expand Down
10 changes: 6 additions & 4 deletions R/order.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ xportr_order <- function(.df, metacore, domain = NULL, verbose = getOption("xpor
}

# Grabs vars from Spec and inputted dataset
vars_in_spec_ds <- metadata[[variable_name]]
vars_in_spec_ds <- metadata[, c(variable_name, order_name)] %>%
arrange(!!sym(order_name)) %>%
extract2(variable_name)

vars_in_spec_ds <- vars_in_spec_ds[!is.na(vars_in_spec_ds)]
# Grabs all variables from Spec file and orders accordingly
ord_vars <- .df %>%
select(all_of(vars_in_spec_ds)) %>%
arrange(!!sym(order_name))
select(any_of(vars_in_spec_ds))

# Variables not in Spec file - will be moved to the end
drop_vars <- .df %>%
select(!all_of(vars_in_spec_ds))
select(!any_of(vars_in_spec_ds))

# Used in warning message for how many vars have been moved
moved_vars <- nrow(drop_vars)
Expand Down
3 changes: 2 additions & 1 deletion R/xportr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#' @importFrom glue glue glue_collapse
#' @importFrom cli cli_alert_info cli_h2 cli_alert_success cli_alert_info
#' cli_div cli_alert_success cli_text cli_h2
#' @importFrom tidyselect all_of
#' @importFrom tidyselect all_of any_of
#' @importFrom utils capture.output str tail packageVersion
#' @importFrom stringr str_detect str_extract str_replace str_replace_all
#' @importFrom readr parse_number
#' @importFrom purrr map_chr map2_chr
#' @importFrom janitor make_clean_names
#' @importFrom tm stemDocument
#' @importFrom graphics stem
#' @importFrom magrittr extract2
"_PACKAGE"

globalVariables(c("abbr_parsed", "abbr_stem", "adj_orig", "adj_parsed", "col_pos", "dict_varname",
Expand Down
68 changes: 27 additions & 41 deletions tests/testthat/test-var-order.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
# library(dplyr)
# library(haven)
# library(testthat)
suppressWarnings({
library(haven)
library(readxl)
})

#
# #context("xportr_seq correctly order dataset according to spec")
#
# test_that("Variables are correctly grabbed from the Spec file", {
#
# spec_manual <- c("STUDYID", "USUBJID","SUBJID","SITEID","AGE","SEX","RACE","ACOUNTRY","FASFL","ITTFL")
#
# spec_xportr <- get_spec_col_names("ADAE", tab_model="ADAM",
# path_to_spec = "~/xportr/inst/specs", vendor = "GSK")[1:10]
#
# expect_equal(spec_manual, spec_xportr)
# })
#
#
# test_that("Variable are ordered correctly", {
#
# ADAE <- read_sas("~/xptr/inst/extdata/adae.sas7bdat")
#
# ADAE_xportr <- xportr_ord("ADAE", ADAE, tab_model = "ADAM",
# path_to_spec = "~/xportr/inst/specs",
# vendor = "GSK", verbose = FALSE,
# msg_var_order = TRUE)
#
# vars_in_spec_ds <- c("STUDYID","SITEID", "USUBJID","SUBJID", "TRT01A", "TRT01AN",
# "AETERM", "AEDECOD","AESOC","AGE","SEX","RACE","SAFFL","ASTDT",
# "ASTTM","AENDT","ADURC","AEACN","AESER","AEOUT", "AEREL","ATOXGR",
# "ATOXGRN","AFTRTSTC")
#
# # Grabs all variables from Spec file and orders accordingly
# seq_vars <- ADAE %>%
# select(all_of(vars_in_spec_ds))
#
# # Variables not in Spec file - will be moved to the end
# drop_vars <- ADAE %>%
# select(!all_of(vars_in_spec_ds))
#
# ADAE_manual <- bind_cols(seq_vars, drop_vars)
#
# expect_equal(ADAE_xportr, ADAE_manual)
# })
#
#

test_that("Variable are ordered correctly", {

ADAE <- read_sas(system.file("extdata", "adae.sas7bdat", package = "xportr"))
met <- read_excel(system.file("specs", "ADaM_spec.xlsx", package = "xportr"), 3)

withr::with_options(
list(xportr.order_name = "Order", xportr.variable_name = "Variable"), {
ADAE_xportr <- xportr_order(ADAE, metacore = met, "ADAE", verbose = "none")
}
)

after_names <- c("STUDYID", "USUBJID", "AEDECOD", "AESOC", "AETERM", "AESER",
"ASTDT", "AENDT", "ATOXGR", "TRT01A", "TRT01AN", "SAFFL", "SUBJID",
"WEIGHTBL", "SEX", "AGE", "AGEU", "RACE", "SITEID", "RACEN",
"ASTTM", "ADURC", "AEACN", "AEOUT", "AEREL", "ATOXGRN", "AFTRTSTC",
"AEWDFL")

expect_equal(names(ADAE_xportr), after_names)
})

0 comments on commit 766e5c2

Please sign in to comment.