From a588fa893bb0a055cdd61dc2cc0100e6e4be082b Mon Sep 17 00:00:00 2001 From: "Thomas S. Dye" Date: Mon, 20 Jun 2022 14:35:26 -1000 Subject: [PATCH] R/S3Classes methods now fail gracefully --- NAMESPACE | 1 - R/S3Classes.R | 150 +++++++++++++++------------- man/reproduce.archaeophases_mcmc.Rd | 2 +- man/reproduce.archaeophases_plot.Rd | 2 +- 4 files changed, 80 insertions(+), 75 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index dd86dd2..a2e0bee 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,6 @@ S3method(original_file,archaeophases_mcmc) S3method(original_file,archaeophases_plot) S3method(plot,archaeophases_plot) -S3method(reproduce,archaeophases_mcmc) S3method(reproduce,archaeophases_plot) export(AgeDepth) export(CreateMinMaxGroup) diff --git a/R/S3Classes.R b/R/S3Classes.R index 77cbc83..e48cbf7 100644 --- a/R/S3Classes.R +++ b/R/S3Classes.R @@ -187,7 +187,7 @@ reproduce <- function(x, ...) { #' Reproduce an MCMC data frame #' #' Reproduces a data frame from metadata held in an \code{archaeophases_mcmc} -#' object. +#' object. Returns NULL if \code{file} is not the original file. #' #' @param x An \code{archaeophases_mcmc} object. #' @param file A path to the original MCMC csv file, or a copy of the file. @@ -205,80 +205,86 @@ reproduce <- function(x, ...) { #' #' @seealso \code{\link{original_file}} #' -#' @export reproduce.archaeophases_mcmc <- function(x, file = NULL, ...) { - if (!original_file(x, file)) - stop("Not the original file.") + if (!original_file(x, file)) { + message("Not the original file.") + return(NULL) + } + else eval(attr(x, "mcmc")) } -#' Reproduce an ArchaeoPhases plot -#' -#' Reproduces a plot from metadata held in an \code{archaeophases_plot} -#' object. -#' -#' @param x An \code{archaeophases_plot} object. -#' @param file Path to the original MCMC csv file, or a copy of the file. -#' @param ... Other parameters. -#' -#' @author Thomas S. Dye, \email{tsd@@tsdye.online} -#' -#' @examples -#' \dontrun{ -#' x <- read_bcal("http://tsdye.online/AP/bc-1.csv") -#' y <- multi_dates_plot(x) -#' z <- reproduce(y) -#' # TRUE -#' identical(y, z) -#' -#' #ERROR, Not the original file. -#' z <- reproduce(y, file = "foo.csv") -#' } -#' -#' @seealso \code{\link{original_file}} -#' -#' @export -reproduce.archaeophases_plot <- function(x, file = NULL, ...) { + #' Reproduce an ArchaeoPhases plot + #' + #' Reproduces a plot from metadata held in an \code{archaeophases_plot} + #' object. Returns NULL if \code{file} is not the original file. + #' + #' @param x An \code{archaeophases_plot} object. + #' @param file Path to the original MCMC csv file, or a copy of the file. + #' @param ... Other parameters. + #' + #' @author Thomas S. Dye, \email{tsd@@tsdye.online} + #' + #' @examples + #' \dontrun{ + #' x <- read_bcal("http://tsdye.online/AP/bc-1.csv") + #' y <- multi_dates_plot(x) + #' z <- reproduce(y) + #' # TRUE + #' identical(y, z) + #' + #' #ERROR, Not the original file. + #' z <- reproduce(y, file = "foo.csv") + #' } + #' + #' @seealso \code{\link{original_file}} + #' + #' @export + reproduce.archaeophases_plot <- function(x, file = NULL, ...) { if (!original_file(x, file)) - stop("Not the original file.") - eval(attr(x, "call")) -} + { + message("Not the original file.") + return(NULL) + } + else + eval(attr(x, "call")) + } -#' Recreate a graphical plot -#' -#' Recreates a graphic from data and metadata held in a -#' \code{archaeophases_plot} object. -#' -#' @details -#' Uses data stored in the \code{archaeophases_plot} object, along with -#' metadata from the call of the plotting function, to recreate the original -#' graphic on the display. -#' -#' @param x An \code{archaeophases_plot} object. -#' @param ... Other parameters. -#' -#' @author Thomas S. Dye, \email{tsd@@tsdye.online} -#' -#' @seealso \code{\link{tempo_plot}} -#' @seealso \code{\link{occurrence_plot}} -#' @seealso \code{\link{marginal_plot}} -#' @seealso \code{\link{multi_marginal_plot}} -#' @seealso \code{\link{tempo_activity_plot}} -#' @seealso \code{\link{multi_dates_plot}} -#' -#' @examples -#' -#' \dontrun{ -#' # Read from connection -#' ox <- read_oxcal("http://tsdye.online/AP/ox.csv") -#' tp_1 <- tempo_plot(ox, position = 1:ncol(ox)) -#' # Recreate the tempo_plot with the original arguments -#' plot(tp_1) -#' } -#' @export -plot.archaeophases_plot <- function(x, ...) { - foo <- as.list(attr(x, "call")) - foo$data <- as.name(deparse(substitute(x))) - foo$position <- NULL - eval(as.call(foo)) -} + #' Recreate a graphical plot + #' + #' Recreates a graphic from data and metadata held in a + #' \code{archaeophases_plot} object. + #' + #' @details + #' Uses data stored in the \code{archaeophases_plot} object, along with + #' metadata from the call of the plotting function, to recreate the original + #' graphic on the display. + #' + #' @param x An \code{archaeophases_plot} object. + #' @param ... Other parameters. + #' + #' @author Thomas S. Dye, \email{tsd@@tsdye.online} + #' + #' @seealso \code{\link{tempo_plot}} + #' @seealso \code{\link{occurrence_plot}} + #' @seealso \code{\link{marginal_plot}} + #' @seealso \code{\link{multi_marginal_plot}} + #' @seealso \code{\link{tempo_activity_plot}} + #' @seealso \code{\link{multi_dates_plot}} + #' + #' @examples + #' + #' \dontrun{ + #' # Read from connection + #' ox <- read_oxcal("http://tsdye.online/AP/ox.csv") + #' tp_1 <- tempo_plot(ox, position = 1:ncol(ox)) + #' # Recreate the tempo_plot with the original arguments + #' plot(tp_1) + #' } + #' @export + plot.archaeophases_plot <- function(x, ...) { + foo <- as.list(attr(x, "call")) + foo$data <- as.name(deparse(substitute(x))) + foo$position <- NULL + eval(as.call(foo)) + } diff --git a/man/reproduce.archaeophases_mcmc.Rd b/man/reproduce.archaeophases_mcmc.Rd index bea0774..494af68 100644 --- a/man/reproduce.archaeophases_mcmc.Rd +++ b/man/reproduce.archaeophases_mcmc.Rd @@ -15,7 +15,7 @@ } \description{ Reproduces a data frame from metadata held in an \code{archaeophases_mcmc} -object. +object. Returns NULL if \code{file} is not the original file. } \examples{ \dontrun{ diff --git a/man/reproduce.archaeophases_plot.Rd b/man/reproduce.archaeophases_plot.Rd index 1ed2391..10371e2 100644 --- a/man/reproduce.archaeophases_plot.Rd +++ b/man/reproduce.archaeophases_plot.Rd @@ -15,7 +15,7 @@ } \description{ Reproduces a plot from metadata held in an \code{archaeophases_plot} -object. +object. Returns NULL if \code{file} is not the original file. } \examples{ \dontrun{