0

a complete newbie to R here. Learning through trial and error. Most bugging issue is saving and loading workspace in the basic R Gui, I haven't started working with R Studio yet. I'm learning as I go. When saving a workspace - which through research I'm told saves all the objects therein - when using the traditional way of File>Save workspace> directory.RData it saves all right to the computer file directory. I also use the command save.image('.RData') when it comes to saving the workspace. The issue is when it comes to loading a previous R session. When I use File>load workspace or even load('<directory.Rdata'>) all I get is a blank screen with this message:

[Previously saved workspace restored]

No data appears below the above message in R. I only manage to retrieve my work if I save it as a text file through Save to file>(directory).txt. Thereafter I open notepad and copy paste to R. This means all graphs drawn in tthe previous session get lost despite this being a tedious job all the time.

I would appreciate if someone advised me step by step on how to save and load work in R. I have tried all online suggestions including from this forum.

use of code to load not working despite multiple enter tries(>)

Ps: when closing basic R Gui, I always click yes to the prompt "Save workspace image" even after saving with the code save.image() or File>save workspace techniques. But still, I can't retrieve my workspace either way. Even when I click 'no' to the prompt, I'm still unable to load workspace in a future R session.

  • 1
    If you want to list what is in your workspace use `ls()`. Loading the data does not also list it. Also be sure that you are in the same directory when you save and load or else use absolute paths. Use `getwd()` to find out what directory is the current one. Also use `file.info("myfile.RData")` where myfile.RData is the name of your file to check the timestamps on your saved data to ensure you are using the one you think you are using. – G. Grothendieck Dec 23 '21 at 14:02
  • 5
    **You should *never* save the workspace in R.** That R even has this ability is widely recognised as a mistake in hindsight, and virtually all experts recommend completely disabling it. Write your code into a file instead (it seems that this is what you are really after, anyway, since the workspace does *not* include the code you wrote; it just includes the variables). – Konrad Rudolph Dec 23 '21 at 14:02
  • 2
    Agree with both, especially with Konrad's recommendation *against* workspace-saving. Note, though, that it doesn't save efforts or printed results, just the objects present in the workspace. This is not a recording of your session, just the state of variables at the moment you saved the workspace. The reasons *against* doing it just like this are numerous but usually follow a theme of "reproducibility" as well as traceability (with just a workspace, noone can see how you defined/derived a variable) and maintainability (similar). – r2evans Dec 23 '21 at 14:21
  • Oh okay. I frequently check my directory using the getwd(). I first set up my working directory with setwd() to always ensure my work will be saved to that specific . It seems I will go with Konrad's view to always save my code in a file, say .txt. I thought R could have a way to load my work, just like the way word can open up a previous document, all inclusive of words, pictures/graphs. From your comments, it seems this is impossible unless of course, I save the code to a .txt file and for graphs, save them to a .jpeg or .pdf file from R Graphics window. Waiting to be corrected. – Samuel Gachuhi Dec 24 '21 at 05:42

0 Answers0