Skip to content

Commit

Permalink
Add with_tempfile functions
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
jimhester committed Aug 31, 2017
1 parent 1ca67be commit 50dbf99
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -41,6 +41,7 @@ Collate:
'seed.R'
'wrap.R'
'sink.R'
'tempfile.R'
'utils.R'
'with.R'
RoxygenNote: 6.0.1
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -15,6 +15,7 @@ export(local_output_sink)
export(local_par)
export(local_path)
export(local_temp_libpaths)
export(local_tempfile)
export(with_)
export(with_collate)
export(with_dir)
Expand All @@ -30,4 +31,5 @@ export(with_path)
export(with_preserve_seed)
export(with_seed)
export(with_temp_libpaths)
export(with_tempfile)
importFrom(stats,runif)
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# devel

- Add `with_tempfile()` and `local_tempfile()` functions to create temporary
files which are cleanup up afterwards. (#32)

- Remove the `code` argument from `local_` functions (#50).

# 2.0.0
Expand Down
31 changes: 31 additions & 0 deletions R/tempfile.R
@@ -0,0 +1,31 @@
#' Temporary files
#'
#' Temporarily create a tempfile, which is automatically removed afterwards.
#' @template with
#' @param new `[character vector]`\cr Names of temporay file handles to create.
#' @param envir `[environment]`\cr Environment in which to define the temporary files.
#' @inheritParams base::tempfile
#' @export
with_tempfile <- function(new, code, envir = parent.frame(),
pattern = "file", tmpdir = tempdir(), fileext = "") {
env <- new.env(parent = envir)
for (f in new) {
assign(f,
tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext),
envir = env)
}
on.exit(unlink(mget(new, envir = env)))
eval(substitute(code), envir = env)
}

#' @rdname with_tempfile
#' @export
local_tempfile <- function(new, envir = parent.frame(),
pattern = "file", tmpdir = tempdir(), fileext = "") {
for (f in new) {
assign(f,
tempfile(pattern = pattern, tmpdir = tmpdir, fileext = fileext),
envir = envir)
}
defer(unlink(mget(new, envir = envir)), envir = envir)
}
37 changes: 37 additions & 0 deletions man/with_tempfile.Rd

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

44 changes: 44 additions & 0 deletions tests/testthat/test-tempfile.R
@@ -0,0 +1,44 @@
context("tempfile")

test_that("with_tempfile works", {

f1 <- character()
f2 <- character()

with_tempfile("file1", {
writeLines("foo", file1)
expect_equal(readLines(file1), "foo")
with_tempfile("file2", {
writeLines("bar", file2)
expect_equal(readLines(file1), "foo")
expect_equal(readLines(file2), "bar")

f2 <<- file2
})
expect_false(file.exists(f2))
f1 <<- file1
})
expect_false(file.exists(f1))
})

test_that("local_tempfile works", {

f1 <- character()
f2 <- character()

f <- function() {
local_tempfile("file1")
writeLines("foo", file1)
expect_equal(readLines(file1), "foo")
local_tempfile("file2")
writeLines("bar", file2)
expect_equal(readLines(file1), "foo")
expect_equal(readLines(file2), "bar")
f1 <<- file1
f2 <<- file2
}
f()

expect_false(file.exists(f1))
expect_false(file.exists(f2))
})

0 comments on commit 50dbf99

Please sign in to comment.