Skip to content

Commit a588fa8

Browse files
committed
R/S3Classes methods now fail gracefully
1 parent e3bb859 commit a588fa8

File tree

4 files changed

+80
-75
lines changed

4 files changed

+80
-75
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
S3method(original_file,archaeophases_mcmc)
44
S3method(original_file,archaeophases_plot)
55
S3method(plot,archaeophases_plot)
6-
S3method(reproduce,archaeophases_mcmc)
76
S3method(reproduce,archaeophases_plot)
87
export(AgeDepth)
98
export(CreateMinMaxGroup)

R/S3Classes.R

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ reproduce <- function(x, ...) {
187187
#' Reproduce an MCMC data frame
188188
#'
189189
#' Reproduces a data frame from metadata held in an \code{archaeophases_mcmc}
190-
#' object.
190+
#' object. Returns NULL if \code{file} is not the original file.
191191
#'
192192
#' @param x An \code{archaeophases_mcmc} object.
193193
#' @param file A path to the original MCMC csv file, or a copy of the file.
@@ -205,80 +205,86 @@ reproduce <- function(x, ...) {
205205
#'
206206
#' @seealso \code{\link{original_file}}
207207
#'
208-
#' @export
209208
reproduce.archaeophases_mcmc <- function(x, file = NULL, ...) {
210-
if (!original_file(x, file))
211-
stop("Not the original file.")
209+
if (!original_file(x, file)) {
210+
message("Not the original file.")
211+
return(NULL)
212+
}
213+
else
212214
eval(attr(x, "mcmc"))
213215
}
214216

215-
#' Reproduce an ArchaeoPhases plot
216-
#'
217-
#' Reproduces a plot from metadata held in an \code{archaeophases_plot}
218-
#' object.
219-
#'
220-
#' @param x An \code{archaeophases_plot} object.
221-
#' @param file Path to the original MCMC csv file, or a copy of the file.
222-
#' @param ... Other parameters.
223-
#'
224-
#' @author Thomas S. Dye, \email{tsd@@tsdye.online}
225-
#'
226-
#' @examples
227-
#' \dontrun{
228-
#' x <- read_bcal("http://tsdye.online/AP/bc-1.csv")
229-
#' y <- multi_dates_plot(x)
230-
#' z <- reproduce(y)
231-
#' # TRUE
232-
#' identical(y, z)
233-
#'
234-
#' #ERROR, Not the original file.
235-
#' z <- reproduce(y, file = "foo.csv")
236-
#' }
237-
#'
238-
#' @seealso \code{\link{original_file}}
239-
#'
240-
#' @export
241-
reproduce.archaeophases_plot <- function(x, file = NULL, ...) {
217+
#' Reproduce an ArchaeoPhases plot
218+
#'
219+
#' Reproduces a plot from metadata held in an \code{archaeophases_plot}
220+
#' object. Returns NULL if \code{file} is not the original file.
221+
#'
222+
#' @param x An \code{archaeophases_plot} object.
223+
#' @param file Path to the original MCMC csv file, or a copy of the file.
224+
#' @param ... Other parameters.
225+
#'
226+
#' @author Thomas S. Dye, \email{tsd@@tsdye.online}
227+
#'
228+
#' @examples
229+
#' \dontrun{
230+
#' x <- read_bcal("http://tsdye.online/AP/bc-1.csv")
231+
#' y <- multi_dates_plot(x)
232+
#' z <- reproduce(y)
233+
#' # TRUE
234+
#' identical(y, z)
235+
#'
236+
#' #ERROR, Not the original file.
237+
#' z <- reproduce(y, file = "foo.csv")
238+
#' }
239+
#'
240+
#' @seealso \code{\link{original_file}}
241+
#'
242+
#' @export
243+
reproduce.archaeophases_plot <- function(x, file = NULL, ...) {
242244
if (!original_file(x, file))
243-
stop("Not the original file.")
244-
eval(attr(x, "call"))
245-
}
245+
{
246+
message("Not the original file.")
247+
return(NULL)
248+
}
249+
else
250+
eval(attr(x, "call"))
251+
}
246252

247-
#' Recreate a graphical plot
248-
#'
249-
#' Recreates a graphic from data and metadata held in a
250-
#' \code{archaeophases_plot} object.
251-
#'
252-
#' @details
253-
#' Uses data stored in the \code{archaeophases_plot} object, along with
254-
#' metadata from the call of the plotting function, to recreate the original
255-
#' graphic on the display.
256-
#'
257-
#' @param x An \code{archaeophases_plot} object.
258-
#' @param ... Other parameters.
259-
#'
260-
#' @author Thomas S. Dye, \email{tsd@@tsdye.online}
261-
#'
262-
#' @seealso \code{\link{tempo_plot}}
263-
#' @seealso \code{\link{occurrence_plot}}
264-
#' @seealso \code{\link{marginal_plot}}
265-
#' @seealso \code{\link{multi_marginal_plot}}
266-
#' @seealso \code{\link{tempo_activity_plot}}
267-
#' @seealso \code{\link{multi_dates_plot}}
268-
#'
269-
#' @examples
270-
#'
271-
#' \dontrun{
272-
#' # Read from connection
273-
#' ox <- read_oxcal("http://tsdye.online/AP/ox.csv")
274-
#' tp_1 <- tempo_plot(ox, position = 1:ncol(ox))
275-
#' # Recreate the tempo_plot with the original arguments
276-
#' plot(tp_1)
277-
#' }
278-
#' @export
279-
plot.archaeophases_plot <- function(x, ...) {
280-
foo <- as.list(attr(x, "call"))
281-
foo$data <- as.name(deparse(substitute(x)))
282-
foo$position <- NULL
283-
eval(as.call(foo))
284-
}
253+
#' Recreate a graphical plot
254+
#'
255+
#' Recreates a graphic from data and metadata held in a
256+
#' \code{archaeophases_plot} object.
257+
#'
258+
#' @details
259+
#' Uses data stored in the \code{archaeophases_plot} object, along with
260+
#' metadata from the call of the plotting function, to recreate the original
261+
#' graphic on the display.
262+
#'
263+
#' @param x An \code{archaeophases_plot} object.
264+
#' @param ... Other parameters.
265+
#'
266+
#' @author Thomas S. Dye, \email{tsd@@tsdye.online}
267+
#'
268+
#' @seealso \code{\link{tempo_plot}}
269+
#' @seealso \code{\link{occurrence_plot}}
270+
#' @seealso \code{\link{marginal_plot}}
271+
#' @seealso \code{\link{multi_marginal_plot}}
272+
#' @seealso \code{\link{tempo_activity_plot}}
273+
#' @seealso \code{\link{multi_dates_plot}}
274+
#'
275+
#' @examples
276+
#'
277+
#' \dontrun{
278+
#' # Read from connection
279+
#' ox <- read_oxcal("http://tsdye.online/AP/ox.csv")
280+
#' tp_1 <- tempo_plot(ox, position = 1:ncol(ox))
281+
#' # Recreate the tempo_plot with the original arguments
282+
#' plot(tp_1)
283+
#' }
284+
#' @export
285+
plot.archaeophases_plot <- function(x, ...) {
286+
foo <- as.list(attr(x, "call"))
287+
foo$data <- as.name(deparse(substitute(x)))
288+
foo$position <- NULL
289+
eval(as.call(foo))
290+
}

man/reproduce.archaeophases_mcmc.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reproduce.archaeophases_plot.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)