Skip to content

Commit

Permalink
source() and example() get catch.abort=FALSE; originating from Luke T…
Browse files Browse the repository at this point in the history
…ierney

git-svn-id: https://svn.r-project.org/R/trunk@85268 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Oct 5, 2023
1 parent e13a5fa commit 48b0ed9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@
\item The matrix multiplication functions \code{crossprod()} and
\code{tcrossprod()} are now also primitive and S3 generic, as
\code{\%*\%} had become in \R 4.3.0.
\item \code{source()} and \code{example()} get a new optional argument
\code{catch.aborts} which allows continued evaluation of the \R code
after an error.
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/library/base/R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function(file, local = FALSE, echo = verbose, print.eval = echo,
max.deparse.length = 150, width.cutoff = 60L,
deparseCtrl = "showAttributes", ## rather? c("keepInteger", "showAttributes", "keepNA"),
chdir = FALSE,
catch.aborts = FALSE,
encoding = getOption("encoding"),
continue.echo = getOption("continue"),
skip.echo = 0, keep.source = getOption("keep.source"))
Expand Down Expand Up @@ -222,7 +223,11 @@ function(file, local = FALSE, echo = verbose, print.eval = echo,
}
}
if (!tail) {
yy <- withVisible(eval(ei, envir))
yy <- if(catch.aborts)
withRestarts(
withVisible(eval(ei, envir)),
abort = function() list(value = NULL, visible = FALSE))
else withVisible(eval(ei, envir))
i.symbol <- mode(ei[[1L]]) == "name"
if (!i.symbol) {
## ei[[1L]] : the function "<-" or other
Expand Down
15 changes: 14 additions & 1 deletion src/library/base/man/source.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ source(file, local = FALSE, echo = verbose, print.eval = echo,
max.deparse.length = 150, width.cutoff = 60L,
deparseCtrl = "showAttributes",
chdir = FALSE,
catch.aborts = FALSE,
encoding = getOption("encoding"),
continue.echo = getOption("continue"),
skip.echo = 0, keep.source = getOption("keep.source"))
Expand Down Expand Up @@ -84,6 +85,8 @@ withAutoprint(exprs, evaluated = FALSE, local = parent.frame(),
\item{chdir}{logical; if \code{TRUE} and \code{file} is a pathname,
the \R working directory is temporarily changed to the directory
containing \code{file} for evaluating.}
\item{catch.aborts}{logical indicating that \dQuote{abort}ing errors
should be caught.}
\item{encoding}{character vector. The encoding(s) to be assumed when
\code{file} is a character string: see \code{\link{file}}. A
possible value is \code{"unknown"} when the encoding is guessed: see
Expand Down Expand Up @@ -191,7 +194,7 @@ if(someCond) withAutoprint({
## If you want to source() a bunch of files, something like
## the following may be useful:
sourceDir <- function(path, trace = TRUE, ...) {
op <- options(); on.exit(options(op)) # to reset after each
op <- options(); on.exit(options(op)) # to reset after each
for (nm in list.files(path, pattern = "[.][RrSsQq]$")) {
if(trace) cat(nm,":")
source(file.path(path, nm), ...)
Expand All @@ -208,6 +211,16 @@ stopifnot(identical(x, 1:2), identical(y, x^2))
withAutoprint({ formals(sourceDir); body(sourceDir) },
max.deparse.length = 20, verbose = TRUE)
%% --> tests in ../../../../tests/eval-etc.R

## Continuing after (catchable) errors:
tc <- textConnection('1:3
2 + "3"
cat(" .. in spite of error: happily continuing! ..\n")
6*7')
r <- source(tc, catch.aborts = TRUE)
## Error in 2 + "3" ....
## .. in spite of error: happily continuing! ..
stopifnot(identical(r, list(value = 42, visible=TRUE)))
}
\keyword{file}
\keyword{programming}
Expand Down
2 changes: 2 additions & 0 deletions src/library/utils/R/example.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function(topic, package = NULL, lib.loc = NULL,
type = c("console", "html"), echo = TRUE, verbose = getOption("verbose"),
setRNG = FALSE, ask = getOption("example.ask"),
prompt.prefix = abbreviate(topic, 6),
catch.aborts = FALSE,
run.dontrun = FALSE, run.donttest = interactive())
{
type <- match.arg(type)
Expand Down Expand Up @@ -128,5 +129,6 @@ function(topic, package = NULL, lib.loc = NULL,
prompt.echo = paste0(prompt.prefix, getOption("prompt")),
continue.echo = paste0(prompt.prefix, getOption("continue")),
verbose = verbose, max.deparse.length = Inf, encoding = "UTF-8",
catch.aborts = catch.aborts,
skip.echo = skips, keep.source=TRUE)
}
3 changes: 3 additions & 0 deletions src/library/utils/man/example.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ example(topic, package = NULL, lib.loc = NULL,
verbose = getOption("verbose"),
setRNG = FALSE, ask = getOption("example.ask"),
prompt.prefix = abbreviate(topic, 6),
catch.aborts = FALSE,
run.dontrun = FALSE, run.donttest = interactive())
}
\arguments{
Expand Down Expand Up @@ -60,6 +61,8 @@ example(topic, package = NULL, lib.loc = NULL,
device and to any devices opened by the example code.}
\item{prompt.prefix}{character; prefixes the prompt to be used if
\code{echo} is true (as it is by default).}
\item{catch.aborts}{logical, passed on to \code{\link{source}()},
indicating that \dQuote{abort}ing errors should be caught.}
\item{run.dontrun}{logical indicating that \verb{\dontrun}
should be ignored.}
\item{run.donttest}{logical indicating that \verb{\donttest}
Expand Down

0 comments on commit 48b0ed9

Please sign in to comment.