Last active
August 29, 2015 14:04
-
-
Save smbache/a528d77f136a4bbf0c19 to your computer and use it in GitHub Desktop.
Toying with declarations of symbols for use with non-standard evaluation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Placeholder function. | |
#' | |
#' @export | |
as_nonstandard <- function() | |
{ | |
stop("This is a placeholder function, and should not be called directly.") | |
} | |
#' Declare symbols for nonstandard evalution. | |
#' | |
#' @param declaration an expression given as \code{symbol <- as_nonstandard()} | |
#' @export | |
declare <- function(declaration) | |
{ | |
d <- substitute(declaration) | |
p <- parent.frame() | |
if (!identical(d[[1]], quote(`<-`))) | |
stop("Declarations follow the <- syntax.", call. = FALSE) | |
if (!is.symbol(d[[2]])) | |
stop("LHS needs to be a symbol", call. = FALSE) | |
sym <- as.character(d[[2]]) | |
msg <- sprintf("Symbol '%s' is declared for non-standard use.", sym) | |
eval( | |
substitute( | |
delayedAssign(x, stop(m, call. = FALSE), environment(), environment()), | |
list(m = msg, x = sym)), p, p) | |
} | |
#' test | |
#' | |
#' @export | |
test <- function() | |
{ | |
declare(Sepal.Length <- as_nonstandard()) | |
subset(iris, Sepal.Length > 5) | |
} | |
#' test2 | |
#' | |
#' @export | |
test2 <- function() | |
{ | |
declare(Sepal.Length <- as_nonstandard()) | |
Sepal.Length | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment