Skip to content

Instantly share code, notes, and snippets.

@AkselA
Created July 1, 2019 16:21
Show Gist options
  • Save AkselA/d47736399e5d8f3783fc2d96fe991b74 to your computer and use it in GitHub Desktop.
Save AkselA/d47736399e5d8f3783fc2d96fe991b74 to your computer and use it in GitHub Desktop.
selfdestructor <- function(x) {
if (is.numeric(x)) {
fn <- paste0("selfdestructor_", x, "k")
} else {
fn <- paste0("selfdestructor_", x)
}
f <- function() {
message("You specified: x = ", x)
rm(list=fn, envir=.GlobalEnv)
message("Poof!")
}
assign(fn, f, envir=.GlobalEnv)
message("Assigned '", fn, "' to global environment.\n",
"You can run ", fn, "(), but be careful: it self-destructs!\n")
gfn <- as.character(match.call())[1]
message("This function, ", gfn, ", will self-destruct in")
for (i in 5:1) {
cat(i, "\n")
system2("sleep", "1")
}
rm(list=gfn, envir=.GlobalEnv)
message("Floof!")
}
selfdestructor("Oh_no")
# Assigned 'selfdestructor_Oh_no' to global environment.
# You can run selfdestructor_Oh_no(), but be careful: it self-destructs!
#
# This function, selfdestructor, will self-destruct in
# 5
# 4
# 3
# 2
# 1
# Floof!
selfdestructor_Oh_no()
# You specified: x = Oh_no
# Poof!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment