How do I start a new .R file default in a new session for new objects in that session?
To access file click File and then load workspace. A dialog box will appear, browse to the folder where you saved the . RData file and click open.
What is the workspace in R? R workspace is temporary space on your CPU's RAM that “disappears” at the end of R session. All data, analyses, output, are all stored as objects in the R workspace. When you exit from R, the temporary space (i.e., workspace) disappears, as do all of your objects.
The quick-and-dirty option is to clear your workspace, quit, and then choose Save. This time, R will be saving an empty workspace which will reload the next time you restart R. The second option is to run: unlink(“. RData”) in the console.
Saving the workspace in R is very easy. In case you want to save the full workspace in R, also known as workspace image (those objects that are displayed when you call the ls function), you can use the save. image function. The data will be saved in a file of type RData (also known as rda ).
Workspaces are .RData files, not .R files. .R files are source files, i.e. text files containing code.
It's a bit tricky. If you saved the workspace, then R saves two files in the current working directory : an .RData file with the objects and a .RHistory file with the history of commands. In earlier versions of R, this was saved in the R directory itself. With my version 2.11.1, it uses the desktop.
If you start up your R and it says : "[Previously saved workspace restored]", then it loaded the file ".RData" and ".RHistory" from the default working directory. You find that one by the command
getwd()
If it's not a desktop or so, then you can use
dir()
to see what's inside. For me that doesn't work, as I only have the file "desktop.ini" there (thank you, bloody Windoze).
Now there are 2 options : you manually rename the workspace, or use the command:
save.image(file="filename.RData")
to save the workspaces before you exit. Alternatively, you can set those options in the file Rprofile.site. This is a text file containing the code R has to run at startup. The file resides in the subdirectory /etc of your R directory. You can add to the bottom of the file something like :
fn <- paste("Wspace",Sys.Date(),sep="")
nfiles <- length(grep(paste(fn,".*.RData",sep=""),dir()))
fn <- paste(fn,"_",nfiles+1,".RData",sep="")
options(save.image.defaults=list(file=fn))
Beware: this doesn't do a thing if you save the workspace by clicking "yes" on the message box. You have to use the command
save.image()
right before you close your R-session. If you click "yes", it will still save the workspace as ".RData", so you'll have to rename it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With