RStudio R File Corruption
Asked Answered
H

2

7

I had a R script open in RStudio. The file was saved many times over the course of several weeks and worked perfectly fine when RStudio was opened and closed. However, today, I restarted my computer and when I opened RStudio and more specifically the script that I mentioned, all of the R code vanished, leaving a single long row of "....." with red highlighting.

enter image description here

When I tried to open the R file in other text processors such as Sublime Text and Notepad++, only a line of zeroes was visible. None of my other R files were affected. I'm currently running Windows 8.1 and have the latest version of R and RStudio. What can I do to recover the code in the file and prevent something like this from happening again?

Huddleston answered 15/1, 2015 at 18:7 Comment(4)
This kind of thing is a major reason I use Github, even for personal projects.Sarita
It happened to me a few times - I think when I did not close rStudio and shut down (closed) my laptop. When next day I tried to open the project, it showed this message WARNING: Failed to restore workspace from '~/CODES/OpenCanada.info/.RData' Reason: bad restore file magic number (file may be corrupted) -- no data loaded. It was the last file that I edited that got corrupted. And Unfortunately, the history file was empty. But then I went to `C:\Users\%user%\AppData\Local\RStudio` and was able to find history there and recover the file from there (as luckily I run it entirely)Symbolize
Related helpful discussion: community.rstudio.com/t/corrupt-r-script/56239/23, reddit.com/r/RStudio/comments/apnu77/corrupted_r_files. The first one links to this package: github.com/jmcphers/rsrecovrSymbolize
See also #75388722 - if you deployed your code to shinyapps, you are lucky, all your codes are there! (as described at support.posit.co/hc/en-us/articles/…)Symbolize
J
8

It's not clear what happened to corrupt your file (and thus how to fix it if possible) and it is kind of ominous that you're just seeing 0's in other text editors, but I'll give you my best suggestion and some tips.

Suggestions for Attempting Recovery

  1. Since your other R files were unaffected, you should have a messy record of your code in the history. Use the history to reconstruct your code.

  2. Access a copy of your file from any version control, cloud, or offline backup you may have used -- git, SVN, iCloud, SugarSync, Dropbox, etc (I realize you probably wouldn't have posted this question if that were an option, but I had to throw it out there).

  3. Use a Hex or sector editor to try to recover the data.

  4. Use a data recover program to find an old version of your file.

  5. Inspect your trash or recycling bin to see if it has an old version. Depending on your OS and the settings of how you (insecurely or securely) delete files, then you may be able to undelete a deleted version, even if it's not immediately available.

  6. Try different methods of recovering text data from corrupted text files like OpenOffice's and Microsoft's suggestions.

Tips for the Future

I know that hindsight is 20/20, but a few quick tips for good measure:

  1. Use version control. Git is supported in RStudio's GUI interface.

  2. Have more than one version of your file. Many professors and professionals recommend writing/storing code in a text editor and using your IDE only for the working copy.

  3. Make backups. Distinct from #2, you should backup your files to a hard drive, flash drive, or cloud service like Dropbox or Spideroak.

Johanna answered 15/1, 2015 at 18:26 Comment(3)
I had a few more links for you, but couldn't add more than 2 links to my solution due to being new. Best of luck!Johanna
Thanks I'll go ahead and try your suggestions. The strange thing is that the file looks untouched in that it is exactly the same size as before, 50KB, and it says that the last time it was modified was when I saved it before restarting...Huddleston
Could the text format been changed? e.g. utf-8 to something else?Illogic
S
12

It might be an old thread and it might have been covered in 'user4458796' answer in suggestion #1 ("Use the history..."), but:

My friend had the same problem and we managed to recover the code from a 'history_database' files located on Windows at: 'C:\Users\%user%\AppData\Local\RStudio-Desktop\'

I assume there is an equivalent location in Linux in general.

Hopefully I won't get downvoted, just sharing my 2cents.

Ben.

Syzygy answered 9/1, 2017 at 17:49 Comment(0)
J
8

It's not clear what happened to corrupt your file (and thus how to fix it if possible) and it is kind of ominous that you're just seeing 0's in other text editors, but I'll give you my best suggestion and some tips.

Suggestions for Attempting Recovery

  1. Since your other R files were unaffected, you should have a messy record of your code in the history. Use the history to reconstruct your code.

  2. Access a copy of your file from any version control, cloud, or offline backup you may have used -- git, SVN, iCloud, SugarSync, Dropbox, etc (I realize you probably wouldn't have posted this question if that were an option, but I had to throw it out there).

  3. Use a Hex or sector editor to try to recover the data.

  4. Use a data recover program to find an old version of your file.

  5. Inspect your trash or recycling bin to see if it has an old version. Depending on your OS and the settings of how you (insecurely or securely) delete files, then you may be able to undelete a deleted version, even if it's not immediately available.

  6. Try different methods of recovering text data from corrupted text files like OpenOffice's and Microsoft's suggestions.

Tips for the Future

I know that hindsight is 20/20, but a few quick tips for good measure:

  1. Use version control. Git is supported in RStudio's GUI interface.

  2. Have more than one version of your file. Many professors and professionals recommend writing/storing code in a text editor and using your IDE only for the working copy.

  3. Make backups. Distinct from #2, you should backup your files to a hard drive, flash drive, or cloud service like Dropbox or Spideroak.

Johanna answered 15/1, 2015 at 18:26 Comment(3)
I had a few more links for you, but couldn't add more than 2 links to my solution due to being new. Best of luck!Johanna
Thanks I'll go ahead and try your suggestions. The strange thing is that the file looks untouched in that it is exactly the same size as before, 50KB, and it says that the last time it was modified was when I saved it before restarting...Huddleston
Could the text format been changed? e.g. utf-8 to something else?Illogic

© 2022 - 2024 — McMap. All rights reserved.