R workspaces, i.e. .R files

How do I create a new default .R file in a new session for new objects in this session?

+4
source share
3 answers

Workspaces are .RData files and not .R..R files are source files, that is, text files containing code.

This is a bit complicated. If you saved the workspace, then R saves two files in the current working directory: the .RData file with objects and the .RHistory file with command history. In earlier versions of R, this was saved in the R folder itself. With my version 2.11.1, it uses the desktop.

If you run your R, and he says: "[Previously saved workspace restored]", then he downloaded the file .RData and .Ristory from the default working directory. You will find it on command

getwd() 

If it is not a desktop or so, then you can use

 dir() 

to see what's inside. This does not work for me, since I only have the file "desktop.ini" (thanks, bloody Windoze).

Now there are 2 options: you manually rename the workspace or use the command:

 save.image(file="filename.RData") 

to save work areas before exiting. Alternatively, you can set these parameters in the Rprofile.site file. This is a text file containing the R code that should be run at startup. The file is located in the / etc subdirectory of your R directory. You can add something like the following to the bottom of the file:

 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 does nothing if you save the workspace by clicking yes in the message box. You must use the command

 save.image() 

before closing the R-session. If you click Yes, it will save the workspace as ".RData", so you will have to rename it again.

+6
source

There is no communication between sessions, objects, and .R control files. In short: not necessary.

You can enjoy watching the processed example at the end of Introduction to R - Sample Session . Run R in your preferred environment and run the commands one by one.

+4
source

I believe that you can save the current workspace using save.image() , which by default will have the name ".RData". You can load the workspace simply by using load() .

If you are loading a pre-existing workspace and do not want this to happen, rename or delete the .RData file in the current working directory.

If you want to have different projects with different workspaces, the easiest way is to create several directories.

+3
source

All Articles