coding in R Studio Console /storing data

For example

d1<- c(x,y)

If I write the above code in the console, I should not use d1 to store anything else in the future? or it will be replaced if I write new code like this one d1<-"klm" ? Even if we clean the console, d1 is permanantly stored?

Other question is if I store a vector in the name of d1, is this an R file? or I have to copy this in the editor and store it as an R file if I have to use it in future.

Disclaimer: This may be the too basic question.

Thanks for the help.

If you use the same name it will change. If you clean the console, doesn't mean that the variable will change. You should use rm(d1) to remove it.

d1 is not saved as a R file until you save it in the next way: save(d1, file="name.DRdata")

To use it again you should use load("name.RData")

Thank you very much. I appreciate it.

This page might be helpful to you: http://stat545.com/block002_hello-r-workspace-wd-project.html
(For maximum benefit, I recommend not only reading through it, but also following along trying things in your console as you go)

Something to keep in mind is that you are working towards writing R script files that will recreate all your important objects (data frames, variables, etc) whenever you run them, rather than relying on saving objects to disk. This is different from working, say, with a spreadsheet where the instructions (the formulas) and the results are all mixed up together in a single file.

For your typical experienced R user, saving R objects to disk individually is actually a fairly rare activity. When it is necessary to do so, I think it's a bit safer to use saveRDS() and readRDS() instead of save() and load().

The reason is that when you load() an object that you saved with save(), it appears in your environment with the same name it had when you originally saved it. If you have some other object in your environment with that name, then it will be replaced with the one you just loaded, with no warning or opportunity to stop the process. By comparison, readRDS() requires you to explicitly assign a name to the result of reading the object back in, so it can't accidentally overwrite something else in your environment.

Here's a very silly example:

save() and load()

my_pet <- "Rover the dog"

# This command shows all the objects in my environment
ls.str()
#> my_pet :  chr "Rover the dog"

save(my_pet, file = "my_dog.Rdata")

# Much later...
my_pet <- "Whiskers the cat"

ls.str()
#> my_pet :  chr "Whiskers the cat"

# Oh wait, I have another pet, too
load("my_dog.Rdata")

# No more Whiskers :-(
ls.str()
#> my_pet :  chr "Rover the dog"

Created on 2018-10-30 by the reprex package (v0.2.1)

saveRDS() and readRDS()

my_pet <- "Rover the dog"

ls.str()
#> my_pet :  chr "Rover the dog"

saveRDS(object = my_pet, file = "my_dog.RDS")

# Much later...
my_pet <- "Whiskers the cat"

ls.str()
#> my_pet :  chr "Whiskers the cat"

# Oh wait, I have another pet, too
my_other_pet <- readRDS("my_dog.RDS")

# Dogs and cats living peacefully together
ls.str()
#> my_other_pet :  chr "Rover the dog"
#> my_pet :  chr "Whiskers the cat"

Created on 2018-10-30 by the reprex package (v0.2.1)

3 Likes

Great. Thank you. This is very help ful

As we do lots of testing it may not be easy to remember all the script file name.
If R programming in R studio has the way to temporarily to use some script file name as users specify and clear it off by one command will be helpful?

Can you describe the problem you’re imagining in more detail?

If you’re asking how to run an entire script at once, that is what the source() command does (there is also a Source button in the strip at the top of an open script file in RStudio).

But your reference to “temporarily” and not being sure users will remember the names of script files makes me concerned that there might be a fundamental confusion about workflows here, so I think it would help if you can describe what you are doing and how you imagining it working some more.

2 Likes

Hello Sorry about the confusion. Let me explain what I meant

for eg:
In R studio console:

> y<-function(z){z+x}
> y(2)
[1] 4

without defining x I ran the code by mistake, but it worked because it used some value that was stored (x<-2) before,
when I did some other testing. I accidently figured it out. This is just an example.
This type of simple code it is easy to understand what was wrong, if it is complex code I was expecting an error as x was not found. I understood now how this works. Thanks

Aha, that example makes things a lot clearer.

This is an important topic, but not actually that basic! If I’m understanding correctly, you are asking about how variable scope works in R?

The simple answer to your specific case: if a variable isn’t found in the environment from which it is called (in this case, the function’s environment), then R looks up one level at a time until it finds a variable with that name. So this works (but can cause problems if you’re not careful!):

x <- 2
f <- function() { x + 1 }

f()
#> [1] 3

If you want to understand how scoping works in R (I encourage it!) this might take some time to work through, but has all the details: http://adv-r.had.co.nz/Functions.html#lexical-scoping

1 Like

Thank you very much. I appreciate your time. Sure I understood how scoping works. But if we are not careful as you mentioned it might mislead. However I still believe if we have alternative options to recall those variable internationally inside the function. If we don’t recall those variable it should produce error by default. Just my thoughts.

This is one of the foundational mechanics in R (language), so it is definitely something you need to get used to.
What I can say is that in practice it is rarely an issue. Another way to make sure you don't make this mistake is linter support in RStudio. You can enable it by going to "Tools -> Global options -> Code -> Diagnostics". One of the options in the pane is "Warn if variable used has no definition in scope". Checking it will produce small yellow triangle on the left-hand side of your code editor. Other options are helpful as well.

2 Likes