Skip to content

Commit

Permalink
Convert S4 methods to S3 and remove plot() function
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jan 17, 2025
1 parent 22aba7e commit 9d1f094
Show file tree
Hide file tree
Showing 28 changed files with 240 additions and 571 deletions.
20 changes: 0 additions & 20 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ License: GPL (>= 3)
URL: https://archaeostat.github.io/archaeocal/
BugReports: https://github.com/ArchaeoStat/ArchaeoCal/issues
Imports:
arkhe (>= 1.9.0),
graphics,
grDevices,
methods,
utils,
V8
Suggests:
Expand All @@ -25,19 +21,3 @@ VignetteBuilder:
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Collate:
'AllClasses.R'
'AllGenerics.R'
'ArchaeoCal-internal.R'
'ArchaeoCal-package.R'
'coerce.R'
'mutators.R'
'oxcal_calibrate.R'
'oxcal_configure.R'
'oxcal_execute.R'
'oxcal_install.R'
'oxcal_parse.R'
'plot.R'
'show.R'
'validate.R'
'zzz.R'
14 changes: 4 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method(as.data.frame,OxCalOutput)
S3method(plot,OxCalOutput)
S3method(as.data.frame,OxCalResults)
S3method(oxcal_parse,OxCalOutput)
S3method(oxcal_parse,character)
export(oxcal_calibrate)
export(oxcal_configure)
export(oxcal_execute)
export(oxcal_install)
exportMethods(oxcal_parse)
exportMethods(plot)
import(arkhe)
importFrom(methods,.valueClassTest)
importFrom(methods,new)
importFrom(methods,setGeneric)
importFrom(methods,setMethod)
importFrom(methods,setValidity)
export(oxcal_parse)
28 changes: 0 additions & 28 deletions R/AllClasses.R

This file was deleted.

57 changes: 0 additions & 57 deletions R/AllGenerics.R

This file was deleted.

23 changes: 23 additions & 0 deletions R/ArchaeoCal-internal.R
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# HELPERS

#' Default value for NULL
#'
#' Replaces `NULL` with a default value.
#' @param x,y An object.
#' @return If `x` is `NULL`, returns `y`; otherwise returns `x`.
#' @keywords internal
#' @noRd
`%||%` <- function(x, y) {
if (is.null(x)) y else x
}

# Reexport from base on newer versions of R to avoid conflict messages
if (exists("%||%", envir = baseenv())) {
`%||%` <- get("%||%", envir = baseenv())
}

assert_exists <- function(x) {
if (length(x) == 1 && !file.exists(x)) {
stop(sprintf("Could not find %s.", x), call. = FALSE)
}
invisible(x)
}
4 changes: 0 additions & 4 deletions R/ArchaeoCal-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@
#' @docType package
#' @keywords internal
"_PACKAGE"

#' @import arkhe
#' @importFrom methods new setGeneric setMethod setValidity .valueClassTest
NULL
43 changes: 26 additions & 17 deletions R/coerce.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# COERCE
#' @include AllGenerics.R
NULL

# @return A [`data.frame`] with the following columns:
# \describe{
# \item{`name`}{}
# \item{`operation`}{}
# \item{`type`}{}
# \item{`date`}{}
# \item{`error`}{}
# \item{`agreement`}{}
# \item{`convergence`}{}
# \item{`likelihood`}{}
# \item{`posterior`}{}
# }
#' Coerce to a Data Frame
#'
#' @param x A [`list`] returned by [`oxcal_parse()`].
#' @param row.names A [`character`] vector giving the row names for the data
#' frame description, or `NULL`.
#' @param optional A [`logical`] scalar. If `FALSE` then the names of the
#' variables in the data frame are checked to ensure that they are
#' syntactically valid variable names and are not duplicated.
#' @param ... Currently not used.
#' @return A [`data.frame`] with the following columns:
#' \describe{
#' \item{`name`}{}
#' \item{`operation`}{}
#' \item{`type`}{}
#' \item{`date`}{}
#' \item{`error`}{}
#' \item{`agreement`}{}
#' \item{`convergence`}{}
#' \item{`likelihood`}{}
#' \item{`posterior`}{}
#' }
#' @export
#' @method as.data.frame OxCalOutput
as.data.frame.OxCalOutput <- function(x, ...) {
as.data.frame.OxCalResults <- function(x, row.names = NULL,
optional = FALSE, ...) {
data.frame(
name = oxcal_get_names(x),
operation = oxcal_get_operation(x),
Expand All @@ -26,6 +33,8 @@ as.data.frame.OxCalOutput <- function(x, ...) {
agreement = oxcal_get_agreement(x),
convergence = oxcal_get_convergence(x),
likelihood = I(oxcal_density(x, prob = "likelihood")),
posterior = I(oxcal_density(x, prob = "posterior"))
posterior = I(oxcal_density(x, prob = "posterior")),
row.names = row.names,
check.names = !optional
)
}
20 changes: 9 additions & 11 deletions R/mutators.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# MUTATORS
#' @include AllGenerics.R
NULL

# Helpers ======================================================================
oxcal_range <- function(x, na.rm = TRUE) {
r <- vapply(
X = x@ocd,
X = x$ocd,
FUN = function(x) {
start_like <- x$likelihood$start
start_post <- x$posterior$start
Expand All @@ -27,7 +25,7 @@ oxcal_range <- function(x, na.rm = TRUE) {
oxcal_density <- function(x, prob = c("likelihood", "posterior")) {
prob <- match.arg(prob, several.ok = FALSE)
lapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x, prob) {
years <- seq.int(
from = x[[prob]]$start,
Expand All @@ -43,41 +41,41 @@ oxcal_density <- function(x, prob = c("likelihood", "posterior")) {

oxcal_get_names <- function(x) {
vapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x) x$name %||% NA_character_,
FUN.VALUE = character(1)
)
}
oxcal_get_operation <- function(x) {
vapply(X = x@ocd[-1], FUN = `[[`, FUN.VALUE = character(1), i = "op")
vapply(X = x$ocd[-1], FUN = `[[`, FUN.VALUE = character(1), i = "op")
}
oxcal_get_type <- function(x) {
vapply(X = x@ocd[-1], FUN = `[[`, FUN.VALUE = character(1), i = "type")
vapply(X = x$ocd[-1], FUN = `[[`, FUN.VALUE = character(1), i = "type")
}
oxcal_get_bp_date <- function(x) {
vapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x) x[["date"]] %||% NA_real_,
FUN.VALUE = numeric(1)
)
}
oxcal_get_bp_error <- function(x) {
vapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x) x[["error"]] %||% NA_real_,
FUN.VALUE = numeric(1)
)
}
oxcal_get_agreement <- function(x) {
vapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x) x$posterior$agreement %||% NA_real_,
FUN.VALUE = numeric(1)
)
}
oxcal_get_convergence <- function(x) {
vapply(
X = x@ocd[-1],
X = x$ocd[-1],
FUN = function(x) x$posterior$convergence %||% NA_real_,
FUN.VALUE = numeric(1)
)
Expand Down
2 changes: 1 addition & 1 deletion R/oxcal_calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' to be calibrated.
#' @param curve A [`character`] string specifying the calibration curve to be
#' used.
#' @return An [`OxCalOutput-class`] object.
#' @return A [`list`] with class `OxCalResults` (see [oxcal_parse()]).
#' @example inst/examples/ex-oxcal-calibrate.R
#' @author N. Frerebeau
#' @family OxCal tools
Expand Down
34 changes: 17 additions & 17 deletions R/oxcal_execute.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#' @param file A [`character`] string naming a file (without extension) to
#' write `script` to. Output files will be named after `file` and written to
#' the same directory.
#' @param mcmc A [`character`] string giving the name of the output file for
#' the MCMC samples (without extension). It must match the `Name` argument of
#' OxCal's [`MCMC_Sample()`](https://intchron.org/tools/oxcalhelp/hlp_commands.html)
#' function. Only used if `script` contains the `MCMC_Sample()` command.
#' @param verbose A [`logical`] scalar: should status updates be displayed?
#' @param ... Further parameters to be passed to [system2()].
#' @return
#' A list with the following elements:
#' A [`list`] with class `OxCalOutput` containing the following elements:
#' \describe{
#' \item{`oxcal`}{A [`character`] string giving the path to the .oxcal file.}
#' \item{`js`}{A [`character`] string giving the path to the .js file.}
Expand All @@ -21,7 +25,7 @@
#' @author N. Frerebeau
#' @family OxCal tools
#' @export
oxcal_execute <- function(script, file = NULL,
oxcal_execute <- function(script, file = NULL, mcmc = "MCMC_Sample",
verbose = getOption("ArchaeoCal.verbose"), ...) {
## Construct output path
if (is.null(file)) {
Expand All @@ -43,24 +47,20 @@ oxcal_execute <- function(script, file = NULL,
out <- oxcal_call(oxcal, ...)
if (verbose) cat(out)

output <- list(
oxcal = oxcal,
js = sprintf("%s.js", file),
log = sprintf("%s.log", file),
txt = sprintf("%s.txt", file)
)

## MCMC ?
csv <- sprintf("%s.csv", file)
## MCMC?
csv <- sprintf("%s.csv", mcmc)
csv <- if (file.exists(csv)) csv else character(0)

## Output files
list(
oxcal = oxcal,
js = sprintf("%s.js", file),
log = sprintf("%s.log", file),
txt = sprintf("%s.txt", file),
csv = csv
structure(
list(
oxcal = oxcal,
js = assert_exists(sprintf("%s.js", file)),
log = assert_exists(sprintf("%s.log", file)),
txt = assert_exists(sprintf("%s.txt", file)),
csv = csv
),
class = "OxCalOutput"
)
}

Expand Down
Loading

0 comments on commit 9d1f094

Please sign in to comment.