GGA
GGA

Reputation: 385

R output of function to environment within function

I'm writing a plugin for Rcmdr, i want to save the output from a function to an object in the current environment.

In R to save the output of a function to an object in the environment given a functiontest():

functiontest() <- function() {
object <- c("2","3")

print(object)
}


my_object <- functiontest()

If i write in my terminal only functiontest() the console returns the output but it doesn't create in my workspace an object called my_object.

Is there a way i can implement a function that when called create and store permanently an object in the environment? I want to write only in my console

functiontest()

and automatically store an object in my environment with the output of that function

Upvotes: 0

Views: 2160

Answers (1)

Derek Darves
Derek Darves

Reputation: 192

As one commenter said, you can "assign" the value to a variable within the global environment. Here's a minimal example:

 functiontest <- function(value, name) {
 x <- value
 assign(name, x)
 }

Or, using your revised example, this should give you the functionality you need.

functiontest <- function() {
  object <<- c("2","3")
  object
}

functiontest()

Upvotes: 1

Related Questions