R workspaces i.e. .R files - r

How do I start a new .R 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.

Related

.Rdata workspace saving in Rstudio

In RStudio, I save my workspace images for different projects (not R projects) as .Rdata files in many different directory locations associated with those projects. The .Rdata files will have a unique prefix that indicates the relevant project/analyses etc. I see that when I set Rstudio to save .Rdata on exit as the default it creates a new .Rdata file in whatever the default working directory is.
My question is this: If I begin in Rstudio by loading a particular .Rdata file is it the case that I have to 'save as' each time I want to resave that particular workspace image? In other words, the default save .Rdata in Rstudio does NOT save the loaded workspace image in the relevant directory with that file name. Is my understanding of this correct?

RStudio opens up with full environmet

when I try to open RStudio, it opens with this environment and I can't open any files because it looks like RStudio is loading this large matrices.
I just want to open RStudio and start with a clean environment and no .rmd files on it
I have aleady deleted all .RData Files and .Rhistory files, uninstalled and installed both R and RStuido, and still, it opens up with all .rmd files and this large matrices on the environment
Ah, yeah, sop go to tools > global options > R General
There you'll see options to restore data on startup. Uncheck those.
If the matrices are so large that it's causing you to run out of RAM, you may need to delete the rproject file (and associated things like .rproj.user. .rhistory, .Rdata, .ruserdata --you may need to enable show hidden files to do that). Or just make a new project and start fresh (copy over the files and data from the broken project directory).

Changing R options persistently

I want to change the prompt in R from > to R> and I know I should use the options command options(prompt="...") and it works, but then when I restart R the prompt is back to >.
Is there anyway to save the change so that it sticks?
Use .Rprofile file:
You can customize the R environment through a site initialization file or a directory initialization file. R will always source the Rprofile.site file first. On Windows, the file is in the C:\Program Files\R\R-n.n.n\etc directory. You can also place a .Rprofile file in any directory that you are going to run R from or in the user home directory.
At startup, R will source the Rprofile.site file. It will then look for a .Rprofile file to source in the current working directory. If it doesn't find it, it will look for one in the user's home directory. There are two special functions you can place in these files. .First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.
More details are here

Same R history from different workspace

I am new to R and I just figured out why my history did not contain all my previous commands. R create a .Rhistory file in each working directory.
I often change working directory and I would like to have the history of all my past sessions in the same file. Is there a simple way to do that ?
Thanks.
(I am on Mac OS 10.6 and I use Rstudio)
An easy way would be to manually save your history like this:
savehistory(file = "~/.Rhistory")
and then load it when you open an R command session:
loadhistory(file = "~/.Rhistory")
Otherwise you can edit your 'Rprofile.site' and add savehistory() and loadhistory() to the functions .Last and .First respectively.
More info about Rprofile.site: here
At startup, R will source the Rprofile.site file. It will then look for a .Rprofile file to source in the current working directory. If it doesn't find it, it will look for one in the user's home directory. There are two special functions you can place in these files. .First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.

Locate the ".Rprofile" file generating default options

In R and RStudio, I think I have messed around with the .Rprofile file a few times, and I currently am loading up an old version of it upon startup of R or RStudio, is there a way that I can quickly find the location of the file that is generating the default options?
Thanks
Like #Gsee suggested, ?Startup has all you need. Note that there isn't just the user profile file, but also a site profile file you could have messed with. And that both files can be found in multiple locations.
You could run the following to list existing files on your system among those listed on the page:
candidates <- c( Sys.getenv("R_PROFILE"),
file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"),
Sys.getenv("R_PROFILE_USER"),
file.path(getwd(), ".Rprofile"),
file.path(Sys.getenv("HOME"), ".Rprofile"))
Filter(file.exists, candidates)
Note that it should be run on a fresh session, right after your started R, so that getwd() will return the current directory at startup. There is also the tricky possibility that your profile files do modify the current directory at startup, in which case you would have to start a "no-profile" session (run R --no-site-file --no-init-file) before running the code above.

Resources