From 26348f72599ed9d12f2e6a14fd0a48695cbb2c1a Mon Sep 17 00:00:00 2001 From: Yilong Zhang Date: Tue, 24 Oct 2023 23:14:07 +0000 Subject: [PATCH] create meta_inherit function --- R/meta_inherit.R | 58 +++++++++++++++++++ man/meta_inherit.Rd | 29 ++++++++++ man/meta_run.Rd | 2 +- .../test-independent-testing-meta_inherit.R | 18 ++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 R/meta_inherit.R create mode 100644 man/meta_inherit.Rd create mode 100644 tests/testthat/test-independent-testing-meta_inherit.R diff --git a/R/meta_inherit.R b/R/meta_inherit.R new file mode 100644 index 0000000..4429744 --- /dev/null +++ b/R/meta_inherit.R @@ -0,0 +1,58 @@ +# Copyright (c) 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. +# All rights reserved. +# +# This file is part of the metalite program. +# +# metalite is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +#' Inherit meta information by keywords +#' +#' @param meta A `meta_adam` object. +#' @param meta_inherit A `meta_adam` object to be inherit. +#' @param name A vector of keywords from `meta_inherit` to `meta_adam`. +#' @param overwrite A logical value to force mapping update. +#' +#' @return A metadata object with population defined. +#' +#' @examples +#' meta_adam(population = r2rtf::r2rtf_adsl, +#' observation = r2rtf::r2rtf_adae) |> +#' meta_inherit(meta_example(), c("apat", "wk12", "ae_summary")) +#' +#' @export +meta_inherit <- function( + meta, + inherit, + name, + overwrite = FALSE +){ + + mapping <- list() + for(i in seq_along(name)){ + x <- collect_adam_mapping(inherit, name[i]) + if(is.null(x)){ + stop(name[i],": keyword is not defined in the `inherit` meta information") + }else{ + mapping[[i]] <- x + } + } + + for(i in seq_along(mapping)){ + if(is.null(collect_adam_mapping(meta, name[[i]])) | overwrite){ + meta[[mapping[[i]][[".location"]]]][[name[[i]]]] <- mapping[[i]] + } + } + + meta +} \ No newline at end of file diff --git a/man/meta_inherit.Rd b/man/meta_inherit.Rd new file mode 100644 index 0000000..8fa87c0 --- /dev/null +++ b/man/meta_inherit.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/meta_inherit.R +\name{meta_inherit} +\alias{meta_inherit} +\title{Inherit meta information by keywords} +\usage{ +meta_inherit(meta, inherit, name, overwrite = FALSE) +} +\arguments{ +\item{meta}{A \code{meta_adam} object.} + +\item{name}{A vector of keywords from \code{meta_inherit} to \code{meta_adam}.} + +\item{overwrite}{A logical value to force mapping update.} + +\item{meta_inherit}{A \code{meta_adam} object to be inherit.} +} +\value{ +A metadata object with population defined. +} +\description{ +Inherit meta information by keywords +} +\examples{ +meta_adam(population = r2rtf::r2rtf_adsl, + observation = r2rtf::r2rtf_adae) |> + meta_inherit(meta_example(), c("apat", "wk12", "ae_summary")) + +} diff --git a/man/meta_run.Rd b/man/meta_run.Rd index d82f35f..3e567e3 100644 --- a/man/meta_run.Rd +++ b/man/meta_run.Rd @@ -20,7 +20,7 @@ Executed analysis based on the analysis plan. Execute analysis based on the analysis plan } \examples{ -if(interactive()){ +if (interactive()) { meta <- meta_example() ae_summary <- function(...) { "results of ae_summary" diff --git a/tests/testthat/test-independent-testing-meta_inherit.R b/tests/testthat/test-independent-testing-meta_inherit.R new file mode 100644 index 0000000..9860f3e --- /dev/null +++ b/tests/testthat/test-independent-testing-meta_inherit.R @@ -0,0 +1,18 @@ + +test_that("Inherit keywords", { + x <- meta_adam(population = r2rtf::r2rtf_adsl, + observation = r2rtf::r2rtf_adae) |> + meta_inherit(meta_example(), c("apat", "wk12", "ae_summary")) + + expect_equal(names(x$population), "apat") + expect_equal(names(x$observation), "wk12") + expect_equal(names(x$analysis), "ae_summary") +}) + +test_that("Wrong key words", { + expect_error( + meta_adam(population = r2rtf::r2rtf_adsl, + observation = r2rtf::r2rtf_adae) |> + meta_inherit(meta_example(), c("apat", "wk12", "ae_ser")) + ) +})