R Script code in R Studio Wiped out, but file still present

Hello Everyone,
I just started using R a week ago, and after making simple code such as for and while loops in R Studio, It works pretty well until I have to save my files.

After I save my R Script as a .R file, It seems as if everything worked out well. I even save the R data and workspace image as well. When I leave the tab of the current file I was coding in, I exited R Studio.

When I log back into R studio, I try to open back up the file I just made. But surprisingly, ALL OF THE CODE IS GONE!! I am very surprised and upset because all the code I made was gone although I saved everything. I am not sure why that is the case. I am using Window 10 and R Studio Version 1.1.383. I also have R installed as well (R Version 4.0.2)

Can somebody please explain what the issue is behind this? Has anybody ever experienced this before? I am very confused.

Try File > Recent File >
File > Open File and navigate to the folder where you saved the file.

Or just go to the directory where you saved the file and open it.

You probably closed the .r file before you closed RStudio.

Thank you for the reply, but unfortunately, it still did not work.

Just off curiosity, I actually saved the file, then I exit the file before I exited RStudio. Do you think that is the reason why my code was wiped out when I go into RStudio?

No I do not think that saving the file is the problem. If yeu had clos-ed the file before exiting RStudio then RStudio would not automatically load it on startup.

I may be misunderstanding the problem. I thought that you just could not find the file. Is that what is happening or is the file actually empty?

If you cannot find it, just go to the operating level and do a disc search. If it exists and is empty my guess is that some how you managed to do a select all and delete just as you were saving. I know this sound impossible but trust me it can be done as I know from personal experience.

The real worry is if the file does not show up is the Recent file list.

What happens if you create a new .r file, muck around with it, maybe save it a couple of times and then exit RStudio and reload?

I am on Ubuntu and have not used Windows in years so I don't have much of an idea how to trouble shoot an R X RStudio X Windows issue.

The problem is that after I write code on a file (Let's say the file name is bob.r), I save the file bob.r on RStudio. After I save it, I closed the file and I closed RStudio. When I go back to RStudio and want to upload and bring back the entire code from bob.r, there is literally nothing on it. All the code is gone. I would like to know if you know some reason behind this. It's not about finding the file, it's about the code being wiped out when I try to load up the file.

Okay, so there is a file bob.r on the hard disc but it is empty? Ouch.

What happens if you open bob.r in a text editor such as Notepad? Also empty?

Try writing an .R file in Notepad or another text editor, and loading it into RStudio, saving etc., and see what happens.

Remember, I know nothing about Windows but if bob.r and the test file are empty I think I would consider re-installing RStudio.

As a rather more desperate move install something like Tinn-R https://sourceforge.net/projects/tinn-r/ and see what happens using something other than RStudio.

After that I would worry about malware

I am sorry I cannot come up with anything more useful. Best wishes.

It occurs to me that you didn't write in this description of your actions that after first creating
a blank new file with a name (Bob.R) , that also after adding code into the R script you saved even once, or any more number of times, after your initial creation of the file?

Okay, so I probably have to write the code in an .r file, but I have to save it as a text file in order for it to save the code itself! I appreciate your help!

No, I was not being clear enough---it was a bit late.

I just was interested in seeing if RSudio's file creation was messed up. I just wanted you to create (robert.r) in Notepad, toss in a couple of R commands, save and then open in RStudio make a few changes and re-save. My idea was to see if RStudio was messing up with the way it was creating the file or how it was saving the file.

BTW nirgrahamuk's suggestion is a very good one.

Strictly speaking a .r file is a text file. The .r extension just allows programs to recognize it as an R script file.

I tried, and it actually worked. I made the code in R, copied and pasted the code into a text file and saved it, and then I pasted the code from the text file into the R file and saved it once again. After that, I am able to open up the R file without any of the contents being wiped out! I appreciate your help!

Okay, that seems like some progress but it was not what I meant. What I hoped you would do is create the text file in Notepad, save it as Rob.r and close Notebook. Then, open RStudio, and do File > Open File > "Rob.r" , make some changes in the file and save the file.

Then reopen Rob.r in Notepad. Have the changes been made?

The changes have been made! All I had to do was switch the (.txt) to (all files .) and then add a .R to the end of the file name. And then I loaded it back up on R, and then it opened up the file successfully. I really appreciate your help. Best wishes!

Excellent. Can you mark it solved?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.