From 3f6cbf3dee648741b3a6f3d351ff9370a59ed7cc Mon Sep 17 00:00:00 2001 From: Tamara Senior Date: Fri, 12 Apr 2024 17:34:42 +0200 Subject: [PATCH 1/4] initial commit --- NAMESPACE | 1 + NEWS.md | 5 ++++- R/metacore.R | 36 ++++++++++++++++++++++++++++++++++++ man/get_keys.Rd | 25 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 man/get_keys.Rd diff --git a/NAMESPACE b/NAMESPACE index 6824bb1..c9023a7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(check_inconsistent_types) export(create_tbl) export(define_to_metacore) export(get_control_term) +export(get_keys) export(is_metacore) export(load_metacore) export(metacore) diff --git a/NEWS.md b/NEWS.md index 10058b6..40be616 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,7 @@ -# Metacroe 0.1.2 +# Metacore 0.1.3 +- Add `get_keys` function which returns the dataset keys for a given dataset + +# Metacore 0.1.2 - Update to resolve issues from the dplyr updates # Metacore 0.1.1 diff --git a/R/metacore.R b/R/metacore.R index 68be91f..d70e9df 100644 --- a/R/metacore.R +++ b/R/metacore.R @@ -413,6 +413,42 @@ get_control_term <- function(metacode, variable, dataset = NULL){ } +#' Get Dataset Keys +#' +#' Returns the dataset keys for a given dataset +#' +#' @param metacode metacore object +#' @param dataset A dataset name +#' +#' @return a 2-column tibble with dataset key variables and key sequence +#' @export +#' +#' @importFrom rlang as_label enexpr as_name +#' +#' @examples +#' \dontrun{ +#' meta_ex <- spec_to_metacore(metacore_example("p21_mock.xlsx")) +#' get_keys(meta_ex, "AE") +#' } +get_keys <- function(metacode, dataset){ + dataset_val <- ifelse(str_detect(as_label(enexpr(dataset)), "\""), + as_name(dataset), as_label(enexpr(dataset))) # to make the filter more explicit + + subset_data <- metacode$ds_vars %>% + filter(dataset == dataset_val) + if(nrow(subset_data) == 0){ + stop(paste0(dataset_val, " not found in the value_spec table. Please check the dataset name")) + } + + keys <- subset_data %>% + filter(!is.na(key_seq)) %>% + select(variable, key_seq) + + keys <- keys[order(keys$key_seq),] + + return(keys) +} + #' save metacore object #' diff --git a/man/get_keys.Rd b/man/get_keys.Rd new file mode 100644 index 0000000..64c6c40 --- /dev/null +++ b/man/get_keys.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/metacore.R +\name{get_keys} +\alias{get_keys} +\title{Get Dataset Keys} +\usage{ +get_keys(metacode, dataset) +} +\arguments{ +\item{metacode}{metacore object} + +\item{dataset}{A dataset name} +} +\value{ +a 2-column tibble with dataset key variables and key sequence +} +\description{ +Returns the dataset keys for a given dataset +} +\examples{ +\dontrun{ +meta_ex <- spec_to_metacore(metacore_example("p21_mock.xlsx")) +get_keys(meta_ex, "AE") +} +} From 86fe5ec11d9a3de17938e8d426e3b064ea27cc08 Mon Sep 17 00:00:00 2001 From: Tamara Senior Date: Fri, 12 Apr 2024 17:57:55 +0200 Subject: [PATCH 2/4] update description --- DESCRIPTION | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index f34b3ab..ddc2f29 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,10 @@ Authors@R: role = "aut", email = "mike.stackhouse@atorusresearch.com", comment = c(ORCID = "0000-0001-6030-723X")), + person(given = "Tamara", + family = "Senior", + role = "aut", + email = "tamara.senior@roche.com"), person(given = "GSK/Atorus JPT", role = c("cph", "fnd"))) Description: Create an immutable container holding metadata for the purpose of better enabling programming activities and functionality of other packages within the clinical programming workflow. From 43a67e704b292788a859076c9dbe90ffd23ef206 Mon Sep 17 00:00:00 2001 From: tamarasenior <102166057+tamarasenior@users.noreply.github.com> Date: Fri, 19 Apr 2024 14:26:07 +0100 Subject: [PATCH 3/4] Update R/metacore.R Co-authored-by: Jana Stoilova <43623360+anajens@users.noreply.github.com> --- R/metacore.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/metacore.R b/R/metacore.R index d70e9df..3bcb754 100644 --- a/R/metacore.R +++ b/R/metacore.R @@ -429,6 +429,7 @@ get_control_term <- function(metacode, variable, dataset = NULL){ #' \dontrun{ #' meta_ex <- spec_to_metacore(metacore_example("p21_mock.xlsx")) #' get_keys(meta_ex, "AE") +#' get_keys(meta_ex, AE) #' } get_keys <- function(metacode, dataset){ dataset_val <- ifelse(str_detect(as_label(enexpr(dataset)), "\""), From ced13e33d958eef9c5b48f6708402f19090c219e Mon Sep 17 00:00:00 2001 From: Tamara Senior Date: Thu, 25 Apr 2024 15:14:22 +0200 Subject: [PATCH 4/4] Write unit tests --- R/metacore.R | 2 +- man/get_keys.Rd | 1 + tests/testthat/test-metacore.R | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/R/metacore.R b/R/metacore.R index 3bcb754..fa0c338 100644 --- a/R/metacore.R +++ b/R/metacore.R @@ -438,7 +438,7 @@ get_keys <- function(metacode, dataset){ subset_data <- metacode$ds_vars %>% filter(dataset == dataset_val) if(nrow(subset_data) == 0){ - stop(paste0(dataset_val, " not found in the value_spec table. Please check the dataset name")) + stop(paste0(dataset_val, " not found in the ds_vars table. Please check the dataset name")) } keys <- subset_data %>% diff --git a/man/get_keys.Rd b/man/get_keys.Rd index 64c6c40..84687b1 100644 --- a/man/get_keys.Rd +++ b/man/get_keys.Rd @@ -21,5 +21,6 @@ Returns the dataset keys for a given dataset \dontrun{ meta_ex <- spec_to_metacore(metacore_example("p21_mock.xlsx")) get_keys(meta_ex, "AE") +get_keys(meta_ex, AE) } } diff --git a/tests/testthat/test-metacore.R b/tests/testthat/test-metacore.R index 840d1ce..a8f3c23 100644 --- a/tests/testthat/test-metacore.R +++ b/tests/testthat/test-metacore.R @@ -137,3 +137,18 @@ test_that("pulling out control terminology works", { ) }) +test_that("get_keys works", { + test <- spec_to_metacore(metacore_example("p21_mock.xlsx"), quiet = TRUE) + #Testing Errors + ## Domain not in ds_vars table + expect_error(get_keys(test, DS)) + ## Missing dataset name + expect_error(get_keys(test)) + #Testing Correct Output + expect_equal( + get_keys(test, DM), + tibble(variable = c("STUDYID", "USUBJID"), key_seq = c(1L, 2L)) %>% + add_labs(variable = "Variable Name", + key_seq = "Sequence Key") + ) +})