2015-05-31 11 views
6

ho scritto alcune macro in R con la funzione defmacro dal pacchetto gtools:È sicuro utilizzare le macro in R?

#' IfLen macro 
#' 
#' Check whether a object has non-zero length, and 
#' eval expression accordingly. 
#' 
#' @param df An object which can be passed to \code{length} 
#' @param body1 If \code{length(df)} is not zero, then this clause is evaluated, otherwise, body2 is evaluated. 
#' @param body2 See above. 
#' @importFrom gtools defmacro 
#' 
#' @examples 
#' ifLen(c(1, 2), { print('yes!') }, {print("no!")}) 
#' 
#' @author kaiyin 
#' @export 
ifLen = gtools::defmacro(df, body1, body2 = {}, expr = { 
      if(length(df) != 0) { 
       body1 
      } else { 
       body2 
      } 
     }) 

#' IfLet macro 
#' 
#' Eval expression x, assign it to a variable, and if that is TRUE, continue 
#' to eval expression1, otherwise eval expression2. Inspired by the clojure 
#' \code{if-let} macro. 
#' 
#' @param sym_str a string that will be converted to a symbol to hold value of \code{x} 
#' @param x the predicate to be evalueated, and to be assigned to a temporary variable as described in \code{sym_str} 
#' @param body1 expression to be evaluated when the temporary variable is TRUE. 
#' @param body2 expression to be evaluated when the temporary variable is FALSE. 
#' @importFrom gtools defmacro 
#' 
#' @examples 
#' ifLet("..temp..", TRUE, {print(paste("true.", as.character(..temp..)))}, 
#'  {print(paste("false.", as.character(..temp..)))}) 
#' 
#' @author kaiyin 
#' @export 
ifLet = gtools::defmacro(sym_str, x, body1, body2={}, expr = { 
      stopifnot(is.character(sym_str)) 
      stopifnot(length(sym_str) == 1) 
      assign(sym_str, x) 
      if(eval(as.symbol(sym_str))) { 
       body1 
      } else { 
       body2 
      } 
     }) 

#' IfLetLen macro 
#' 
#' Similar to ifLet, but conditioned on whether the length of 
#' the result of \code{eval(x)} is 0. 
#' 
#' 
#' @param x the predicate to be evalueated, and to be assigned to a temporary var called \code{..temp..} 
#' @param body1 expression to be evaluated when \code{..temp..} is TRUE. 
#' @param body2 expression to be evaluated when \code{..temp..} is FALSE. 
#' @importFrom gtools defmacro 
#' 
#' @examples 
#' ifLetLen("..temp..", 1:3, {print(paste("true.", as.character(..temp..)))}, 
#'  {print(paste("false.", as.character(..temp..)))}) 
#' 
#' @author kaiyin 
#' @export 
ifLetLen = gtools::defmacro(sym_str, x, body1, body2={}, expr = { 
      stopifnot(is.character(sym_str)) 
      stopifnot(length(sym_str) == 1) 
      assign(sym_str, x) 
      ifLen(eval(as.symbol(sym_str)), { 
       body1 
      }, { 
       body2 
      }) 
     }) 

C'è qualche pericolo nel loro utilizzo?

+2

Mai nemmeno sentito parlare di loro. Prendo che generano il codice al volo. Perché dovresti usarli al posto di una funzione, soprattutto considerando le potenti capacità di R di guardare il proprio codice? –

+1

Esiste il potenziale pericolo che le macro non abbiano il proprio ambiente. Vedi l'articolo di Thomas Lumley R-news menzionato nella pagina di aiuto ([PDF Warning] (http://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf)) per maggiori informazioni. – Jota

risposta

5

Ovviamente bisogna stare attenti. Le macro non hanno i loro ambienti.

semplice esempio f (x, y) = (x + y)^2:

m1 <- defmacro(x, y, expr = { 
    x <- x + y 
    x*x 
}) 

esecuzione m1 cambierà le variabili di input nell'ambiente chiamante:

> x <- 1 
> y <- 2 
> m1(x, y) 
[1] 9 
> x 
[1] 3 
> y 
[1] 2 

funzioni R Normal comportarsi in modo diverso:

f1 <- function(x, y) { 
    x <- x + y 
    x*x 
} 

> x <- 1 
> y <- 2 
> f1(x, y) 
[1] 9 
> x 
[1] 1 
> y 
[1] 2 
+0

Ben illustrato. Potresti dare un'occhiata a questo? http://stackoverflow.com/questions/30562653/defmacro-that-uses-local-variables-in-r – qed