2011-05-12 77 views
5

是否有R A “this” 引用,让我来写,而不是R函数中是否有“this”引用?

envir1 <- new.env() 
assign("x", 4, envir=envir1) 

test <- function(env1) { 
    environment(this) <- env1 
    return(x + 5) 
} 

test(envir1) 

envir1 <- new.env() 
assign("x", 4, envir=envir1) 

test2 <- function() { 
    return(x+1) 
} 

test <- function(env1) { 
    environment(test2) <- env1 
    return(test2()) 
} 

test(envir1) 
+1

没有必要,以结束与每个语句分号。这是R,而不是C;) – 2011-05-12 15:01:01

+0

好吧,你是正确的.. thx ^^ – tObi 2011-05-12 15:12:03

+0

@Joris - 现在修正:-) – 2011-05-12 16:03:11

回答

10

怎么样

test <- function(env1) { 
    with(env1, { 
     return(x + 5); 
     }) 
} 
+0

不错..多数民众赞成在我正在寻找。谢谢 – tObi 2011-05-12 15:08:04