Kieran Martin
Kieran Martin

Reputation: 127

Preserve a promise in R

I want to, essentially, pass a value untouched through a function. So in the following example (in Rstudio):

example_function <- function(datain){
   as.environment("package:utils")$View(datain)
} 

I want the inner function to act as if I'm passing it the original object, in particular so the name which appears in the View window will have the name of the original object (X, say) rather than datain which is what currently occurs.

Upvotes: 1

Views: 73

Answers (2)

Bertil Baron
Bertil Baron

Reputation: 5003

You can also use get for this.

example_function <- function(datain){
  as.environment("package:utils")$View(get(datain),datain)
}

in this case you don't pass the variable but rather the name of the variable as a string.

example_function("X")

Upvotes: 0

Cihan
Cihan

Reputation: 2307

With deparse(substitute(datain)) you can get the original name of the argument passed.

Then, to accomplish what you asked for, you can simply do

example_function <- function(datain){
  as.environment("package:utils")$View(datain, deparse(substitute(datain)))
}  

Now the View window will be titled appropriately as you wanted.

However note that "I want the inner function to act as if I'm passing it the original object" request of yours is not possible in R. R does not support pass-by-reference. There are some workarounds, but if you only needed if for naming the View, the above fix should be fine.

Upvotes: 1

Related Questions