2015-05-31 43 views
6

我已經寫了使用從gtoolsdefmacro功能R中的一些宏:在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 
      }) 
     }) 

是否有使用它們的危險嗎?

+2

從未聽說過它們。我認爲他們在飛行中生成代碼。爲什麼你會使用這些而不是一個函數,特別是考慮到R的強大功能來查看自己的代碼? –

+1

宏有沒有自己的環境的潛在危險。有關詳細信息,請參閱幫助頁面上提及的Thomas Lumley R-news文章([PDF Warning](http://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf))。 – Jota

回答

5

當然你必須小心。宏沒有自己的環境。

簡單實施例F(X,Y)=(X + Y)^ 2:

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

執行m1將改變在調用環境輸入變量:

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

正常R裏面的函數行爲有所不同:

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

很好地說明了。你也可以看看這個嗎? http://stackoverflow.com/questions/30562653/defmacro-that-uses-local-variables-in-r – qed

相關問題