Skip to content

Instantly share code, notes, and snippets.

@renkun-ken
Last active August 25, 2016 12:07
Show Gist options
  • Save renkun-ken/5aa628f5ab4f24b81847372d4111b01a to your computer and use it in GitHub Desktop.
Save renkun-ken/5aa628f5ab4f24b81847372d4111b01a to your computer and use it in GitHub Desktop.
hide-source
e <- new.env()
makeActiveBinding("private_f", function(x) {
if (missing(x)) {
NULL
} else {
sum <- 0
for (i in 1:x) {
sum <- sum + i
}
e$res <- sum
}
}, e)
public_f <- function(x) {
e$private_f <- x
e$res
}
hide_src <- function(f) {
env <- attr(attr(f, "srcref"), "srcfile")
env$lines <- ""
env$filename <- ""
env$original$lines <- ""
f
}
secure <- function(f) {
e <- new.env(parent = environment(f))
eval(substitute(makeActiveBinding("private", function(x) {
if (missing(x)) NULL
else e$result <<- f
}, e), list(f = body(f))))
rm(f)
function(x) {
e$private <- x
e$result
}
}
#' ftest
#' @name ftest
#' @export
NULL
delayedAssign("ftest", secure(function(x) x + 100), environment(), environment())
secret <- function(expr) {
expr <- substitute(expr)
e <- new.env(parent = parent.frame())
e$result <- NULL
private <- function(x) {}
body(private) <- substitute({
if (missing(x)) NULL
else result <<- expr
}, list(expr = expr))
environment(private) <- e
makeActiveBinding("private", private, e)
lockEnvironment(e)
public <- function(x) {
private <<- x
result
}
environment(public) <- e
rm(expr, private, e)
public
}
secure <- function(f) {
e <- new.env(parent = environment(f))
eval(substitute(makeActiveBinding("private", function(x) {
if (missing(x)) NULL
else e$result <- f
}, e), list(f = body(f))))
rm(f)
function(x) {
e$private <- x
e$result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment