Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
version 1.0.1
  • Loading branch information
jimhester authored and cran-robot committed Feb 4, 2016
1 parent 236e3b3 commit 63dc02f
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 38 deletions.
14 changes: 8 additions & 6 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Encoding: UTF-8
Package: withr
Title: Run Code 'With' Temporarily Modified Global State
Version: 1.0.0
Version: 1.0.1
Authors@R: c(
person("Jim", "Hester", , "james.f.hester@gmail.com", role = c("aut", "cre")),
person("Kirill", "Müller", , "krlmlr+r@mailbox.org", role = "aut"),
Expand All @@ -10,24 +10,26 @@ Authors@R: c(
person("RStudio", role = "cph"))
Description: A set of functions to run code 'with' safely and temporarily
modified global state. Many of these functions were originally a part of the
devtools package, this provides a simple package with limited dependencies
'devtools' package, this provides a simple package with limited dependencies
to provide access to these functions.
URL: http://github.com/jimhester/withr
BugReports: http://github.com/jimhester/withr/issues
Depends: R (>= 3.2.0)
Depends: R (>= 3.0.2)
License: GPL (>= 2)
LazyData: true
Imports: stats, graphics
Suggests: testthat
Collate: 'with_.R' 'collate.R' 'dir.R' 'env.R' 'libpaths.R' 'locale.R'
'makevars.R' 'options.R' 'par.R' 'path.R' 'with.R'
'makevars.R' 'options.R' 'par.R' 'path.R' 'wrap.R' 'sink.R'
'with.R'
RoxygenNote: 5.0.1
NeedsCompilation: no
Packaged: 2015-09-22 14:09:44 UTC; jhester
Packaged: 2016-02-04 15:42:19 UTC; jhester
Author: Jim Hester [aut, cre],
Kirill Müller [aut],
Hadley Wickham [aut],
Winston Chang [aut],
RStudio [cph]
Maintainer: Jim Hester <james.f.hester@gmail.com>
Repository: CRAN
Date/Publication: 2015-09-23 02:58:41
Date/Publication: 2016-02-04 17:58:33
37 changes: 21 additions & 16 deletions MD5
@@ -1,5 +1,6 @@
3e3e49bd6b3810dff3d147f59b55319e *DESCRIPTION
8a8866237a58a71109bffd5e804cd22f *NAMESPACE
80886f95f2c000c4b8de9953f45faa17 *DESCRIPTION
8c9b6c98df3a7ac762169c0fbe6985c1 *NAMESPACE
07aedf3a1dff792bd57b488bb1a65c80 *NEWS.md
b3ec11ee76b339c4d2ae9e409721039c *R/collate.R
13ed0fd71ee35cae21d9da32ef81d00b *R/dir.R
e202b586b599ebd825c856567615b8c7 *R/env.R
Expand All @@ -9,20 +10,24 @@ d5e3df31efc160218e5e41f47d70f00a *R/locale.R
3a7ad0bd8fc47af29778f0dcde2d8551 *R/options.R
7ae96d23abe7df158000f77a73aa1580 *R/par.R
a02e5528c7efb2d98360ecd6e19bb488 *R/path.R
854311eab2a12fd10875eada311fe13d *R/with.R
141667060bdebd7df4f64cf222926837 *R/sink.R
e594acf46da18766818a47c92820882b *R/with.R
3d3bf7e65f26dc616bc20d93b1b92ec9 *R/with_.R
88e44ec61deb387dd1c2d8a607c420ee *R/wrap.R
18c40f182effc974e243c3de3ec95913 *README.md
067657b82299b89e25e53ca495971039 *man/with_.Rd
aaf62288d27e94453cfce2d13307d410 *man/with_collate.Rd
b47a0dc1a4ac3d73f847fb59630bc9c6 *man/with_dir.Rd
ff8dce05b6ca94365958b6103b231d4b *man/with_envvar.Rd
af3e16397a72f524a2744904e1ff4f90 *man/with_libpaths.Rd
0494ea90dd564c5c71bbe67ed7e1182a *man/with_locale.Rd
f4dfe6bebf345bcff907ccc37199ce0f *man/with_makevars.Rd
cafcfa50789f3a406dcc0f6eac7bafe1 *man/with_options.Rd
7b70bf28ebfd6e1ddf3816c1a7903583 *man/with_par.Rd
1ccc9b2e2416b0bd240e095b69cabaea *man/with_path.Rd
2c5715d993e51361c602f84ea900a03f *man/with_temp_libpaths.Rd
5e00c27bc53e265f5a40d4ce4811888f *man/withr.Rd
29000dbdef2470c50ed7fcd428272c05 *man/with_.Rd
e004bf86411e3021a37e3859625d5084 *man/with_collate.Rd
c94a2337fbe854529f7d4126c1e2b07a *man/with_dir.Rd
815437f9ada3c2a392240a5a68f06f63 *man/with_envvar.Rd
7c812b458155b87f1454b21beba86ba5 *man/with_libpaths.Rd
14e4745b18998007d2675ec056ba89e5 *man/with_locale.Rd
90cc043b6c978a60adb1995d35b6551d *man/with_makevars.Rd
049733066a626635021b6a2b09f61add *man/with_options.Rd
b022e5b9b3c042adf806a00571d1c9b7 *man/with_par.Rd
74bf724782cd546869a094bce6f2a157 *man/with_path.Rd
aa43d30a4a0404881041b3c7b193a000 *man/with_sink.Rd
8225733328b46b818df0e6b467f0434c *man/with_temp_libpaths.Rd
dc4ff003917591c5db30c32a80f17c5c *man/withr.Rd
70c4d334a0974e15d0309c48ca52ca08 *tests/testthat.R
ea54ff80af0d3d21359aa5127aa25378 *tests/testthat/test-with.R
a71a65191ace70d64dd670ebf8f8bdb2 *tests/testthat/test-sink.R
05d85471f04a5f64c1974cd5400a5cc4 *tests/testthat/test-with.R
4 changes: 3 additions & 1 deletion NAMESPACE
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

export(with_)
export(with_collate)
Expand All @@ -7,7 +7,9 @@ export(with_envvar)
export(with_libpaths)
export(with_locale)
export(with_makevars)
export(with_message_sink)
export(with_options)
export(with_output_sink)
export(with_par)
export(with_path)
export(with_temp_libpaths)
7 changes: 7 additions & 0 deletions NEWS.md
@@ -0,0 +1,7 @@
# 1.0.1
- Relaxed R version requirement to 3.0.2 (#35, #39).
- New `with_output_sink()` and `with_message_sink()` (#24).

# 1.0.0

First Public Release
95 changes: 95 additions & 0 deletions R/sink.R
@@ -0,0 +1,95 @@
# sink -----------------------------------------------------------------------

# FIXME: Use (a better version of) pryr:::partial2 when available
output_sink <- function(file = NULL, append = FALSE, split = FALSE) {
sink(file = file, append = append, type = "output", split = split)
}

message_sink <- function(file = NULL, append = FALSE) {
sink(file = file, append = append, type = "message", split = FALSE)
}

#' @include wrap.R
set_output_sink <- wrap(
output_sink,
if (is.null(file)) {
stop("file cannot be NULL", call. = FALSE)
},
list(n = sink.number()))

set_message_sink <- wrap(
message_sink,
{
if (is.null(file)) {
stop("file cannot be NULL,", call. = FALSE)
}
if (sink.number(type = "message") != 2L) {
stop("Cannot establish message sink when another sink is active.",
call. = FALSE)
}
con <- if (is.character(file)) {
file <- file(file, if (append) "a" else "w")
}
},
{
list(n = sink.number(type = "message"), con = con)
})

reset_output_sink <- function(sink_info) {
repeat {
n <- sink.number()
delta <- n - sink_info$n

if (delta >= 0L) {
sink()

if (delta > 0L) {
warning("Removing a different sink.", call. = FALSE)
} else {
return()
}
} else {
warning("Sink #", sink_info$n, " already removed.", call. = FALSE)
return()
}
}
}

reset_message_sink <- function(sink_info) {
if (!is.null(sink_info$con)) {
on.exit(close(sink_info$con), add = TRUE)
}

do_reset_message_sink(sink_info)
}

do_reset_message_sink <- function(sink_info) {
n <- sink.number(type = "message")
if (n == 2L) {
warning("No message sink to remove.", call. = FALSE)
} else if (n == sink_info$n) {
sink(type = "message")
} else {
warning("Not removing a different message sink.", call. = FALSE)
}
}

#' Output redirection
#'
#' Temporarily divert output to a file via \code{\link{sink}}. For
#' sinks of type \code{message}, an error is raised if such a sink is already
#' active.
#'
#' @template with
#' @param new \code{[character(1)|connection]}\cr
#' A writable \link{connection} or a character string naming the file to write
#' to. Passing \code{NULL} will throw an error.
#' @inheritParams base::sink
#' @seealso \code{\link{sink}}
#' @export
#' @name with_sink
with_output_sink <- with_(set_output_sink, reset_output_sink)

#' @rdname with_sink
#' @export
with_message_sink <- with_(set_message_sink, reset_message_sink)
3 changes: 2 additions & 1 deletion R/with.R
Expand Up @@ -26,7 +26,8 @@
#' \item \code{\link{with_makevars}}: Makevars variables
#' \item \code{\link{with_options}}: options
#' \item \code{\link{with_par}}: graphics parameters
#' \item \code{\link{with_path}}: PATH environment variable
#' \item \code{\link{with_path}}: \code{PATH} environment variable
#' \item \code{\link{with_sink}}: output redirection
#' }
#' @section Creating new "with" functions:
#' All \code{with_} functions are created by a helper function,
Expand Down
24 changes: 24 additions & 0 deletions R/wrap.R
@@ -0,0 +1,24 @@
wrap <- function(f, pre, post, envir = parent.frame()) {
fmls <- formals(f)

# called pass all extra formals on
called_fmls <- stats::setNames(lapply(names(fmls), as.symbol), names(fmls))

f_call <- as.call(c(substitute(f), called_fmls))
pre <- substitute(pre)
post <- substitute(post)

fun <- eval(bquote(function(args) {
.(pre)
.retval <- .(f_call)
.(post)
}, as.environment(list(f_call = f_call, pre = pre, post = post))))

# substitute does not work on arguments, so we need to fix them manually
formals(fun) <- fmls

environment(fun) <- envir

fun

}
2 changes: 1 addition & 1 deletion man/with_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_collate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_envvar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_libpaths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_locale.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_makevars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_par.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions man/with_sink.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_temp_libpaths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/withr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63dc02f

Please sign in to comment.