开发者

R workspaces i.e. .R files

开发者 https://www.devze.com 2023-01-14 20:10 出处:网络
How do I start a new .R 开发者_JAVA技巧file default in a new session for new objects in that session?Workspaces are .RData files, not .R files. .R files are source files, i.e. text files containing co

How do I start a new .R 开发者_JAVA技巧file default in a new session for new objects in that session?


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.


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

If you're loading a pre-existing workspace and you don't want that 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 thing to do is create multiple directories.


There is no connection between sessions, objects and controlling files .R. In short: no need to.

You may enjoy walking through the worked example at the end of the Introduction to R - A Sample Session. Fire up R in your preferred environment and execute the commands one-by-one.

0

精彩评论

暂无评论...
验证码 换一张
取 消