Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "this" reference in R Functions?

Tags:

r

Is there a "this" reference in R that allows me to write

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

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

test(envir1)

instead of:

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

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

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

test(envir1)
like image 544
tObi Avatar asked May 12 '11 14:05

tObi


People also ask

What does %% mean in R?

%% gives Remainder. %/% gives Quotient. So 6 %% 4 = 2. In your example b %% a , it will vectorised over the values in “a”

What does '~' mean in R?

Tilde operator is used to define the relationship between dependent variable and independent variables in a statistical model formula. The variable on the left-hand side of tilde operator is the dependent variable and the variable(s) on the right-hand side of tilde operator is/are called the independent variable(s).

Does R assign by reference?

R passes everything by reference until you modify it. R creates a copy when you modify the object. You should always keep all the Object modifications in same function.

How do you check if a vector contains an element in R?

%in% operator can be used in R Programming Language, to check for the presence of an element inside a vector. It returns a boolean output, evaluating to TRUE if the element is present, else returns false.


1 Answers

how about

test <- function(env1) {
    with(env1, {
        return(x + 5);
        })
}
like image 147
kohske Avatar answered Sep 27 '22 21:09

kohske