hans glick
hans glick

Reputation: 2591

How to use an outside built variable in my function in R

I've been struggling for a while with this : I try to use a variable which is built in the global environnement inside my function code.

Here is my code :

adressbrouillon="C/Data/..."
load_all <- function() {
load(paste(adressbrouillon,"work.RData",sep=""))
}

When I run :

load_all()

Nothing happens.

However, when I run this command :

load(paste(adressbrouillon,"work.RData",sep=""))

That's works very well!

Can anyone tell me what's happen and what to do to get my load_all function work? Thanks in advance!

Upvotes: 1

Views: 60

Answers (1)

Dominic Comtois
Dominic Comtois

Reputation: 10401

Try this:

load_all <- function() {
    load(paste(adressbrouillon,"work.RData",sep=""), envir = .GlobalEnv)
}

If you don't specify envir, the data is loaded into a temporary environment which is destroyed when the function returns.

Upvotes: 3

Related Questions