Skip to content

Commit

Permalink
create meta_inherit function
Browse files Browse the repository at this point in the history
  • Loading branch information
elong0527 committed Oct 24, 2023
1 parent a2fbab2 commit 26348f7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
58 changes: 58 additions & 0 deletions R/meta_inherit.R
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#' 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
}
29 changes: 29 additions & 0 deletions man/meta_inherit.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/meta_run.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/test-independent-testing-meta_inherit.R
Original file line number Diff line number Diff line change
@@ -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"))
)
})

0 comments on commit 26348f7

Please sign in to comment.