-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
with_tempfile #32
Comments
Honestly, I think A function |
Ditching the with_tempfile <- function(new, code, envir = parent.frame()) {
env <- new.env(parent = envir)
for (f in new) {
assign(f, tempfile(), envir = env)
}
on.exit(unlink(mget(new, envir = env)))
eval(substitute(code), envir = env)
}
with_tempfile("tmp1", {
writeLines(con = tmp1, "hi1")
print(readLines(tmp1))
print(tmp1)
with_tempfile("tmp2", {
writeLines(con = tmp2, "hi2")
print(readLines(tmp1))
print(readLines(tmp2))
print(tmp1)
print(tmp2)
})
})
#> [1] "hi1"
#> [1] "/tmp/RtmpjcsoCQ/filecc56e5cb51e"
#> [1] "hi1"
#> [1] "hi2"
#> [1] "/tmp/RtmpjcsoCQ/filecc56e5cb51e"
#> [1] "/tmp/RtmpjcsoCQ/filecc551757b92" |
This is nice. I wonder what the factual difference between Will there be any "no visible global binding for ..." warnings with that code? |
I don't think there is any real difference between There will be the global binding warnings, but that would be true for any of the methods no? One can always register them with f <- function() {
with_tempfile("tmp1", {
writeLines(con = tmp1, "hi1")
print(readLines(tmp1))
})
}
codetools::findGlobals(f, merge = F)$variables
#> [1] "tmp1" |
There will be no warning with scoping functions (#33):
|
I like both interfaces: with_ and scope_. I'm thinking about a way to support both, for cases such as with_tempfile(). |
This takes an idea from
bquote()
and allows you to specify what variable to assign the temporary filename to in your code by surrounding it in a.()
call. Then these files are cleaned up afterwards. The function definition and example usage is below. An alternative interface would be to have a separate argument to specify variable names to use, but I think it is a little nicer to specify them inline. Any thoughts @krlmlr?The text was updated successfully, but these errors were encountered: