diff --git a/DESCRIPTION b/DESCRIPTION index fe0431d..a9af153 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: hrmn Title: Harmonize Datasets -Version: 0.0.0.9000 +Version: 0.0.0.9001 Authors@R: person("Jon", "Harmon", , "jonthegeek@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4781-4346")) @@ -20,4 +20,6 @@ Config/testthat/edition: 3 Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.3 \ No newline at end of file +RoxygenNote: 7.3.3 +Imports: + S7 diff --git a/NAMESPACE b/NAMESPACE index 6ae9268..9ebe023 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,4 @@ # Generated by roxygen2: do not edit by hand +export(specify_fct) +if (getRversion() < "4.3.0") importFrom("S7", "@") diff --git a/R/specify_fct.R b/R/specify_fct.R new file mode 100644 index 0000000..02562ad --- /dev/null +++ b/R/specify_fct.R @@ -0,0 +1,34 @@ +# Developer note: `specify_fct()` is designed to create a data-less +# "specification" object. It defines the target state (the levels) for a factor +# but doesn't hold any actual factor data itself. This is why the constructor +# internally provides `integer()` as the data component to `S7::new_object()`. +# +# In the future, we might use the `hrmn_fct` class to represent actual, +# harmonized factor data. In that scenario, we would likely create a separate +# `class_hrmn_fct` object and have `specify_fct()` be a wrapper function that +# calls the constructor with the empty data. For now, since we don't need the +# full factor-like class, we are directly defining the `hrmn_fct` class in +# `specify_fct()` + +#' Specify a factor harmonization +#' +#' Create a `hrmn_fct` object that specifies the desired levels for a factor +#' variable. This 'specification' object does not contain any data itself, only +#' the rules for harmonization. +#' +#' @param levels (`character`) The allowed values of the factor. +#' @returns A factor specification, an S7 object of class `hrmn::hrmn_fct`. +#' @export +specify_fct <- S7::new_class( + "hrmn_fct", + parent = S7::class_factor, + properties = list( + levels = S7::class_character + ), + constructor = function(levels = character()) { + S7::new_object( + integer(), + levels = levels + ) + } +) diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..2b31dbb --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,3 @@ +# enable usage of @name in package code +#' @rawNamespace if (getRversion() < "4.3.0") importFrom("S7", "@") +NULL diff --git a/man/specify_fct.Rd b/man/specify_fct.Rd new file mode 100644 index 0000000..f460874 --- /dev/null +++ b/man/specify_fct.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/specify_fct.R +\name{specify_fct} +\alias{specify_fct} +\title{Specify a factor harmonization} +\usage{ +specify_fct(levels = character()) +} +\arguments{ +\item{levels}{(\code{character}) The allowed values of the factor.} +} +\value{ +A factor specification, an S7 object of class \code{hrmn::hrmn_fct}. +} +\description{ +Create a \code{hrmn_fct} object that specifies the desired levels for a factor +variable. This 'specification' object does not contain any data itself, only +the rules for harmonization. +} diff --git a/tests/testthat/test-dummy.R b/tests/testthat/test-dummy.R deleted file mode 100644 index e8941f7..0000000 --- a/tests/testthat/test-dummy.R +++ /dev/null @@ -1,3 +0,0 @@ -test_that("Delete this test", { - succeed("Give the action something to process.") -}) diff --git a/tests/testthat/test-specify_fct.R b/tests/testthat/test-specify_fct.R new file mode 100644 index 0000000..f966f88 --- /dev/null +++ b/tests/testthat/test-specify_fct.R @@ -0,0 +1,14 @@ +test_that("specify_fct() returns an object with the correct class", { + spec <- specify_fct() + expect_s3_class( + spec, + c("hrmn::hrmn_fct", "factor", "S7_object"), + exact = TRUE + ) +}) + +test_that("specify_fct() stores the levels", { + lvls <- c("a", "b", "c") + spec <- specify_fct(levels = lvls) + expect_equal(spec@levels, lvls) +})