1

Let's say I have the following function:

create_X <- function() {

  x <- 14

}

Is there a way to give back the x variable to the console? So I can just hit x in my r console and then 14 pops up?

2 Answers2

2

Maybe this could help

create_X <- function() {
  assign("x", 14, envir=globalenv())
}

create_X()
print(x)
#14
2

You could also carefully use

create_X <- function() {
  x <<- 14
}

create_X()
print(x)

About this operator, see also How do you use "<<-" (scoping assignment) in R?

Community
  • 1
  • 1
Vongo
  • 1,325
  • 1
  • 17
  • 27