Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R workspaces i.e. .R files

Tags:

r

How do I start a new .R file default in a new session for new objects in that session?

like image 789
Georgette Avatar asked Aug 31 '10 19:08

Georgette


People also ask

Where are my saved workspaces in R?

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 an R workspace file?

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.

How do I delete previously saved workspace in R?

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.

How do I save a file in R workspace?

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 ).


1 Answers

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.

like image 54
Joris Meys Avatar answered Oct 02 '22 10:10

Joris Meys