3

I'm new to R but am slowing getting the hang of the basics. However I'm struggling to save and then open my work. I am saving as an .RData file however the file appearis in Windows Explorer simple as a 'File'. When trying to load a workspace into R these files only show when I change 'Files of type' from '.RData' to 'All files' and it will refuse to open these files. What am I doing wrong?

Also - if this were to work correctly would this method be similar to opening up a saved Word document - in that everything is the same as it was when I saved it and I can just continue my work?

Many thanks

James
  • 105
  • 2
  • 3
  • 8
  • How exactly are you saving? Are you using the default R GUI or RStudio? What does "refuse" to open the files mean exactly? How are you trying to open them? Remember, R is a programming language, it is not a document editor. – MrFlick Nov 28 '15 at 02:56
  • Saving by 'Save workspace' and using the default R GUI. I'm trying to open them by using 'Open workspace', when I try to open a file it just come up with a blank notepad-style thing... When I go to open the workspace they are no longer .RData files, just 'files'. – James Nov 28 '15 at 03:01

2 Answers2

3

In R you can save two types of files, .RData which is called the workspace and contains all objects (data) you created in a session. You can check what is in the workspace by typing ls() into the console. The other type is a .R file which is a simple script file and saves your code.

If you are working with the default R GUI, it helps to save an .RData file in your working directory, i.e. where your input data files are located, and load your R session from there by double clicking the .RData icon. Then open your script file (.R) from within the R GUI. Usually, I would not never save anything in my workspace, unless it is computationally intensive and takes minutes or longer to calculate by running the code in the script file.

Also make sure that when saving workspace files for the first time to keep the file ending (.RData) in the file name. It usually shows up highlighted in blue and once you type your file name its being deleted. Keeping it will identify the workspace file with an R icon. If you saved it without the .RData ending, you can also add it manually by clicking the saved workspace file and add .RData to the file name. When saving script files you actually have to add the ending yourself (.R).

So, the reason I am writing all this is because you said you clicked 'Open workspace', which doesn't exist to my knowledge. It's called 'Load workspace'. Did you perhaps try to open an .RData file by choosing 'Open script' instead?

I would have asked my last paragraph in the comment section but don't have enough reputation to comment. So let me know if this doesn't solve your problem and I will delete the answer.

Stefan
  • 727
  • 1
  • 9
  • 24
  • Thanks for your reply. Sorry yeah I meant to say 'Load workspace' rather than 'Open workspace'. The problem seems to be stemming from the fact that I am ostensibly saving my workspace as a .RData file, but it straight away becomes just a 'file'. When I try to open these files from Windows Explorer it asks me 'How do you want top open this file?' and gives a list of programmes. If I choose to open with R GUI it comes with 'ARGUMENT '(file location)'_ignored_ – James Nov 28 '15 at 15:12
  • OK, I can reproduce this error by saving my workspace **without** the .RData extension. Then my workspace file just looks like a "file". When I double click it, I can navigate to open it with R GUI and it will give me the same error that you get. But when I hit "OK" R will still start up though. Can you confirm this? So for me, for there are 2 ways to make it work. **1)** Making sure your preserve the .RData extension when saving your workspace, or **2)** click the workspace file that looks like a "file" and manually add the .RData extension to the file name. Then hit okay and "Open with" R. – Stefan Nov 28 '15 at 22:21
  • Yep R will start up after clicking "OK", but will just open a blank workspace. It seems impossible for me to preserve the .RData extension when saving the workspace...it just straight away becomes just a 'file'. However editing the name and adding .RData does indeed seem to help. Although it doesn't show me all my previous code/calculations etc (it's just blank other than the intro garb and '[previously saved workspace restored]' - but I can continue where I left off. Is this how it's supposed to be or I should I be able to see all the code from my last sessions? Thanks for your help by theway – James Nov 29 '15 at 01:41
  • That's good news that you got it working. But again the workspace file (.RData) will **not** show your code or your calculations. It's just a place where you can save the results of your codes and calculations (i.e data). Instead you should save an R script file (.R) which is your actual working document. That's where you can save code and make notes etc. Also I would suggest switching to [R Studio](https://www.rstudio.com/), which is much more user friendly. If my answers helped you, you might want to accept this as an answer to your question. – Stefan Nov 29 '15 at 02:14
  • 1
    Awesome, it all makes sense now. I've realised I was causing a problem by not ensuring .RData was added to the end of the file name when I changed the name. Thanks for your help and explanations, I'll accept this as an answer. – James Nov 29 '15 at 02:50
1

I am writing a R code which uses for saving a workspace: suppose we have a object name x, we want to save it in D drive and I want to save it names as zian.

x<-c(1,2,3,4)
write.table(x,"D:\\zian.txt")
  #here I saves a workspace as a txt file
   write.csv(x,"D:\\zian.csv")
  #here I save a workspace as a csv file 
   #or
   save(object,filename="D:\\2nd")
   #here you have a file in D drive name as 2nd and you want to save this object in this file.
Sorif Hossain
  • 1,231
  • 1
  • 11
  • 18