.Rdata workspace saving in Rstudio - r

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?

Related

RStudio: Running .Rprofile in source file location

I have set up a workflow governed by a Makefile.
Under code/ I have multiple *.r scripts each typically responsible for creating one output file (typically an RData file but could also be csv exports or png images, any file in principle)
code/.Rprofile contains some helper functions to bootstrap the whole project directory system and sources some helper functions etc.
The scripts in code/ need this functionality to work properly.
RStudio has the convenient menu entry to set working directory to source file location.
But could I also make it run .Rprofile in that directory if found? Or really just start R a fresh from the directory of the source file?

loading downloaded data to Rstudio

mission on online course:
Download this RData file to your working directory. Then load the data into R with the following command:
load("skew.RData")
so I have downloaded it to my computer but where is my working directory? or how do I load the downloaded file to Rstudio
You can see your working directory by executing getwd().
In order to load the data you need to change it to the folder path where the data is stored.
setwd("C:/your/path")
load("skew.RData")
You can set you working directory in RStudio on the right side manually and then save the files there and open them like :
load("skew.RData")
If your file is saved somewhere else, you should define the path to it:
load("path/to/your/file/skew.RData")

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

How do you edit a .rds file in RStudio?

I am trying to run an R script that I've inherited from a colleague. This script references a .rds file called config.rds. It stores some configuration settings. I need to change those settings. However, when I attempt to open the file in the Rstudio editor, a "Load R object" prompt pops up. I cannot figure out how to open the file for editing.
You can't open the file for editing - it is a binary file that stores the internal representation of R data objects.
You can only really read it into R to create a new R object, and then save a modified copy of that R object into a new or (the same) .RDS file. Example:
config = readRDS("config.rds")
config$username = "fnord"
saveRDS(config, "config.rds")

R workspaces i.e. .R files

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.

Resources