diff --git a/NAMESPACE b/NAMESPACE index 9b933b0..7758217 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,9 @@ # Generated by roxygen2: do not edit by hand +S3method(get_L_unit,adm) +S3method(get_L_unit,multiadm) +S3method(get_T_unit,adm) +S3method(get_T_unit,multiadm) S3method(get_completeness,adm) S3method(get_completeness,multiadm) S3method(get_height,adm) @@ -26,6 +30,10 @@ S3method(plot,adm) S3method(plot,multiadm) S3method(print,adm) S3method(print,multiadm) +S3method(set_L_unit,adm) +S3method(set_L_unit,multiadm) +S3method(set_T_unit,adm) +S3method(set_T_unit,multiadm) S3method(strat_to_time,phylo) S3method(summary,adm) S3method(summary,multiadm) @@ -33,6 +41,8 @@ S3method(time_to_strat,phylo) export(L_axis_lab) export(T_axis_lab) export(add_adm_to_multiadm) +export(get_L_unit) +export(get_T_unit) export(get_completeness) export(get_height) export(get_hiat_duration) @@ -48,6 +58,8 @@ export(is_destructive) export(merge_adm_to_multiadm) export(merge_multiadm) export(sedrate_to_multiadm) +export(set_L_unit) +export(set_T_unit) export(split_multiadm) export(strat_cont_to_multiadm) export(strat_to_time) diff --git a/R/T_and_L_unit_handling.R b/R/T_and_L_unit_handling.R new file mode 100644 index 0000000..6d5c931 --- /dev/null +++ b/R/T_and_L_unit_handling.R @@ -0,0 +1,146 @@ +get_L_unit = function(x, ...){ + + #' + #' @export + #' + #' @title extract length unit + #' + #' @description + #' extracts the length unit from adm or multiadm object + #' + #' @param x adm or multiadm object + #' @param ... other parameters + #' + #' @returns character - the length unit of `x` + #' + #' @seealso [get_T_unit()] [set_L_unit()] + # + + UseMethod("get_L_unit") +} + +get_L_unit.adm = function(x, ...){ + + #' @export + + return(x$L_unit) +} + +get_L_unit.multiadm = function(x, ...){ + + #' @export + + return(x$L_unit) +} + +set_L_unit = function(x, L_unit, ...){ + + #' + #' @export + #' + #' @title set length units + #' + #' @description + #' set length units for adm and multiadm objects + #' + #' + #' @param x adm or multiadm object + #' @param L_unit time unit + #' @param ... further parameters + #' + #' @returns an adm or multiadm object with the L unit assigned + #' + #' @seealso [set_T_unit()] [get_L_unit()] + #' + + UseMethod("set_L_unit") +} + +set_L_unit.adm = function(x, L_unit, ...){ + + #' @export + + x$L_unit = L_unit + return(x) +} + +set_L_unit.multiadm = function(x, L_unit, ...){ + + #' @export + #' + x$L_unit = L_unit + return(x) +} + +get_T_unit = function(x, ...){ + + #' + #' @export + #' + #' @title extract Time unit + #' + #' @description + #' extracts the Time unit from adm or multiadm object + #' + #' @param x adm or multiadm object + #' @param ... other parameters + #' + #' @returns character - the time unit of `x` + #' + #' @seealso [set_T_unit()] [get_L_unit()] + + UseMethod("get_T_unit") +} + +get_T_unit.adm = function(x, ...){ + + #' @export + + return(x$T_unit) +} + +get_T_unit.multiadm = function(x, ...){ + + #' @export + + return(x$T_unit) +} + +set_T_unit = function(x, T_unit, ...){ + + #' + #' @export + #' + #' @title set time units + #' + #' @description + #' set time units for adm and multiadm objects + #' + #' + #' @param x adm or multiadm object + #' @param T_unit time unit + #' @param ... further parameters + #' + #' @returns an adm or multiadm object with the time unit assigned + #' + #' @seealso [set_L_unit()] [get_T_unit()] + #' + + UseMethod("set_T_unit") +} + +set_T_unit.adm = function(x, T_unit, ...){ + + #' @export + + x$T_unit = T_unit + return(x) +} + +set_T_unit.multiadm = function(x, T_unit, ...){ + + #' @export + #' + x$T_unit = T_unit + return(x) +} \ No newline at end of file diff --git a/R/add_adm_to_multiadm.R b/R/add_adm_to_multiadm.R index 7485751..80daec0 100644 --- a/R/add_adm_to_multiadm.R +++ b/R/add_adm_to_multiadm.R @@ -18,8 +18,12 @@ add_adm_to_multiadm = function(x, ...){ x[["t"]][[x$no_of_entries + i]] = inlist[[i]]$t x[["h"]][[x$no_of_entries + i]] = inlist[[i]]$h x[["destr"]][[x$no_of_entries + i]] = inlist[[i]]$destr - x[["T_unit"]][[x$no_of_entries + i]] = inlist[[i]]$T_unit - x[["L_unit"]][[x$no_of_entries + i]] = inlist[[i]]$L_unit + if (x$T_unit != get_T_unit(inlist[[i]])){ + stop("Inconsistent time units, can not merge") + } + if (x$L_unit != get_L_unit(inlist[[i]])){ + stop("Inconsistent length units, can not merge") + } } x$no_of_entries = x$no_of_entries + length(inlist) diff --git a/R/merge_adm_to_multiadm.R b/R/merge_adm_to_multiadm.R index f395144..035b2bd 100644 --- a/R/merge_adm_to_multiadm.R +++ b/R/merge_adm_to_multiadm.R @@ -18,14 +18,22 @@ merge_adm_to_multiadm = function(...){ "no_of_entries" = length(adm_list), "T_unit" = NULL, "L_unit" = NULL) + T_units = unlist(sapply(adm_list, get_T_unit)) + L_units = unlist(sapply(adm_list, get_L_unit)) + + if (any(rep(T_units[1], length(adm_list)) != T_units)){ + stop("Inconsistent time units, can not merge adms") + } + if (any(rep(L_units[1], length(adm_list)) != L_units)){ + stop("Inconsistent length units, can not merge adms") + } + for (i in seq_along(adm_list)){ adm = adm_list[[i]] multiadm[["t"]][[i]] = adm$t multiadm[["h"]][[i]] = adm$h multiadm[["destr"]][[i]] = adm$destr - } - class(multiadm) = "multiadm" return(multiadm) } diff --git a/man/get_L_unit.Rd b/man/get_L_unit.Rd new file mode 100644 index 0000000..406c859 --- /dev/null +++ b/man/get_L_unit.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/T_and_L_unit_handling.R +\name{get_L_unit} +\alias{get_L_unit} +\title{extract length unit} +\usage{ +get_L_unit(x, ...) +} +\arguments{ +\item{x}{adm or multiadm object} + +\item{...}{other parameters} +} +\value{ +character - the length unit of \code{x} +} +\description{ +extracts the length unit from adm or multiadm object +} +\seealso{ +\code{\link[=get_T_unit]{get_T_unit()}} \code{\link[=set_L_unit]{set_L_unit()}} +} diff --git a/man/get_T_unit.Rd b/man/get_T_unit.Rd new file mode 100644 index 0000000..e4087bc --- /dev/null +++ b/man/get_T_unit.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/T_and_L_unit_handling.R +\name{get_T_unit} +\alias{get_T_unit} +\title{extract Time unit} +\usage{ +get_T_unit(x, ...) +} +\arguments{ +\item{x}{adm or multiadm object} + +\item{...}{other parameters} +} +\value{ +character - the time unit of \code{x} +} +\description{ +extracts the Time unit from adm or multiadm object +} +\seealso{ +\code{\link[=set_T_unit]{set_T_unit()}} \code{\link[=get_L_unit]{get_L_unit()}} +} diff --git a/man/set_L_unit.Rd b/man/set_L_unit.Rd new file mode 100644 index 0000000..1865249 --- /dev/null +++ b/man/set_L_unit.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/T_and_L_unit_handling.R +\name{set_L_unit} +\alias{set_L_unit} +\title{set length units} +\usage{ +set_L_unit(x, L_unit, ...) +} +\arguments{ +\item{x}{adm or multiadm object} + +\item{L_unit}{time unit} + +\item{...}{further parameters} +} +\value{ +an adm or multiadm object with the L unit assigned +} +\description{ +set length units for adm and multiadm objects +} +\seealso{ +\code{\link[=set_T_unit]{set_T_unit()}} \code{\link[=get_L_unit]{get_L_unit()}} +} diff --git a/man/set_T_unit.Rd b/man/set_T_unit.Rd new file mode 100644 index 0000000..0f95f92 --- /dev/null +++ b/man/set_T_unit.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/T_and_L_unit_handling.R +\name{set_T_unit} +\alias{set_T_unit} +\title{set time units} +\usage{ +set_T_unit(x, T_unit, ...) +} +\arguments{ +\item{x}{adm or multiadm object} + +\item{T_unit}{time unit} + +\item{...}{further parameters} +} +\value{ +an adm or multiadm object with the time unit assigned +} +\description{ +set time units for adm and multiadm objects +} +\seealso{ +\code{\link[=set_L_unit]{set_L_unit()}} \code{\link[=get_T_unit]{get_T_unit()}} +} diff --git a/tests/testthat/test_T_and_L_unit_handling.R b/tests/testthat/test_T_and_L_unit_handling.R new file mode 100644 index 0000000..6b9da80 --- /dev/null +++ b/tests/testthat/test_T_and_L_unit_handling.R @@ -0,0 +1,37 @@ +test_that("Setting and getting of T unit for adm works", { + T_unit = "cats" + adm = tp_to_adm(t = 1:3, h = 1:3) + expect_equal(get_T_unit(set_T_unit(x = adm, T_unit = T_unit)), T_unit) + adm = tp_to_adm(t = 1:3, h = 1:3, T_unit = T_unit) + expect_equal(get_T_unit(adm), T_unit) + +}) + +test_that("Setting and getting of L unit for adm works", { + L_unit = "dogs" + adm = tp_to_adm(t = 1:3, h = 1:3) + expect_equal(get_L_unit(set_L_unit(x = adm, L_unit = L_unit)), L_unit) + adm = tp_to_adm(t = 1:3, h = 1:3, L_unit = L_unit) + expect_equal(get_L_unit(adm), L_unit) + +}) + +test_that("setting and getting of T unit for multiadm works", { + T_unit = "cats" + madm = merge_adm_to_multiadm(tp_to_adm(t = 1:3, h = 1:3)) + expect_equal(get_T_unit.multiadm(set_T_unit.multiadm(x = madm, T_unit = T_unit)), T_unit) + madm = merge_adm_to_multiadm(tp_to_adm(t = 1:3, h = 1:3, T_unit = T_unit)) + madm$T_unit = T_unit + expect_equal(get_T_unit(madm), T_unit) + +}) + +test_that("setting and getting of L unit for multiadm works", { + L_unit = "dogs" + madm = merge_adm_to_multiadm(tp_to_adm(t = 1:3, h = 1:3)) + expect_equal(get_L_unit.multiadm(set_L_unit.multiadm(x = madm, L_unit = L_unit)), L_unit) + madm = merge_adm_to_multiadm(tp_to_adm(t = 1:3, h = 1:3, L_unit = L_unit)) + madm$L_unit = L_unit + expect_equal(get_L_unit(madm), L_unit) + +}) diff --git a/tests/testthat/test_merge_adm_to_multiadm.R b/tests/testthat/test_merge_adm_to_multiadm.R index 1de3b7c..2f51878 100644 --- a/tests/testthat/test_merge_adm_to_multiadm.R +++ b/tests/testthat/test_merge_adm_to_multiadm.R @@ -4,3 +4,17 @@ test_that("Produces multiadm object",{ multiadm = merge_adm_to_multiadm(adm1, adm2) expect_true(is_multiadm(multiadm)) }) + +test_that("Inconsistent time units throw error", { + adm1 = tp_to_adm(t = 1:3, h = 2:4, T_unit = "cats") + adm2 = tp_to_adm(t = 1:3, h = 2:5, T_unit = "dogs") + expect_error(merge_adm_to_multiadm(adm1, adm2)) + +}) + +test_that("Inconsistent length units throw error", { + adm1 = tp_to_adm(t = 1:3, h = 2:4, L_unit = "cats") + adm2 = tp_to_adm(t = 1:3, h = 2:5, L_unit = "dogs") + expect_error(merge_adm_to_multiadm(adm1, adm2)) + +})