Help accessing code inside corrupted R markdown files?

Hello everyone,

I am in the middle of writing my dissertation using R studio. This morning I had a crash that corrupted two markdown (Rmd) files and I am desperate to try to access the code at least inside of them.

When I try to open them inside R studio, I get an error in the shell that reads:
Unable to establish communication with R session when executing 'open session'

and then in the console it reads:
Error: embedded nul in string: '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0

I understand that this may have been caused because I tried to save while R studio was crashing.

I can't open the Rmd files in word or a text editor because they are binary - I am wondering if there is any chance that there is a way to access just the code blocks. It's thousands of lines of code that are critical to my dissertation.

I've learned an important lesson about back ups but it would be an absolute lifesaver if there was a way to get those files open and retrieve the code.

Thank you inadvance!

It might be possible depending on your toolkit. See this S/O post. Try it on a copy first, yes?

Hi @jpsherman,
If you ran those corrupted files recently then the code should be in the .Rhistory file in your working directory. You can also copy-paste the code from the pane in RStudio.
Good luck.

** EDIT: sorry for mis-direction, that doesn't appear to work for .Rmd files.

1 Like

Hi DavoWW, I super appreciate your suggestion and willingness to help. Sadly it does not work.
Jess

Hello Technocrat,
Thank you! I will give this a try. If it works I will let you know. I will 100% try on a copy first!
Jess

Rmd files are not binary, they are just text files, if you are on Windows you can open them with Notepad.

Thanks - I have opened my files in EMACS and VI on a unix system and they contain only ^@ repeated over an over. Running cat or more on them prompts that they are probably a binary file, which is why I was confused. I guess that then this means that these files themselves are useless?

I'm afraid so, unless you are experiencing an extreme encoding issue which seems unlikely.

Try this from a terminal

tr < file-with-nulls -d '\000' > file-without-nulls