Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch to a new environment and stick into it?

Tags:

r

I'm building an analysis workflow for large datasets but first I have to validate it on smaller scale datasets. What I'd like to do is to separate my "sampled" datasets from the actual datasets by putting them in an environment like this:

sample_data<-new.env()
attach(sample_data)
# downloading sample_data sets
sample_df_1 <- some_download_function(parameters1)
sample_df_2 <- some_download_function(parameters2)
...
# doing some stuff with them
...

However when I do this, sample_df_1 and sample_df_2 will be stored in global environment rather than my sample_data environment. Of course I can use assign(..., envir=sample_data) but that is somewhat tedious, and I don't want them to show up in the final code.

It is also not ideal to use with because the lines of code inside it cannot be executed one by one, which makes it rather inconvenient at developing stage.

What I hope to achieve is the same kind of behaviour as debug and undebug, for example:

switch_to_env(sample_data)
# Everything done here will be done within environment "sample_data"
# And the lines of codes here can be executed one by one
switch_to_env(.GlobalEnv)

as @Gregor pointed out, "setting an option" probably better describes what I'm looking for: an option that allows user to specify the environment in which the R REPL evaluates expressions.

Thanks!

like image 504
Benny Avatar asked Oct 13 '15 17:10

Benny


People also ask

How long does it take to adapt to a new environment?

How long does it take to adapt to a new environment? A study published in the European Journal of Social Psychology claimed that it takes about 66 days for a new behavior to become a habit. So, it takes about two and half months to adapt well to a new work environment and get used to the behaviors and routine.

Why is it important to adjust to a new environment?

Adaptation is essential in order to to survive and move ahead in the world. The ability to adapt to people, situations and surroundings affords people a greater opportunity to get what they want and what they need.


2 Answers

I think you can define a new operator which always assign value inside your desired environment

`%=%` = function(var,value){
 e <<- new.env()
 varname = deparse(substitute(var))
 assign(varname,value,envir = e)
}

then every time you want to store a var in that environment, you can use:

x %=% 1

in order you can fetch the var in that environment, you can use:

attach(e)

to put that var in your searching path

like image 181
cloudscomputes Avatar answered Oct 13 '22 17:10

cloudscomputes


This isn't exactly what you're looking for, but I think it's workable (and safe).

Whenever you want to develop in a new environment, open a new file and new R session, (say, sample_data.R), source() a script that creates whatever objects you want in the parent environment, and do your development as normal.

When you want to access these within a specific environment from your real global environment you can do this (in your original R session/environment)

sample_data<-new.env()
source("sample_data.R", local = sample_data)

It has some cons: very inconvenient for frequent switching back and forth, especially if your code takes much time to run. However, I actually like that it makes you separate code for different arguments into different files - this sounds like a potentially buggy system if you get mixed up what's where and in which environment you're working. Having separate files offers some protection by enforcing code separation. It also makes your various sub-environments easy to omit from a run---commenting out the source() line in your main file switches off the whole sub-environment.

like image 44
Gregor Thomas Avatar answered Oct 13 '22 17:10

Gregor Thomas