Content of .R file deleted after saving

Greetings.

It's the third or so time I run to this issue and it is getting rather frustrating.
Same issue as described by others in [Content of .R file deleted - not the file, but the content]. I hit save on the script, everything looks OK on the source pane, but after logging off everything is deleted in the script. I run RStudio 2022.02.2 Build 485 and the issue has occurred with both R.4.1.1 & R.4.1.2 and UTF-8 and ISO-8859-7 encodings. I even double checked that I have indeed saved the .R file, just in case. Still after trying to reopen the .R file, no line of code, 0 bytes saved.

Any thoughts on what might have happened?

Thank you.

I ran into this same issue.

Unfortunately, I had the RStudio autosave feature turned on, and wound up losing about two weeks of work in the span of about 3 minutes.

There seem to be some major conflicts with R Studio and R 4.2.0 (at least on Windows).

I rolled back to R 4.1 and haven't lost any more work since.

I'd also like to know if any answers at some point. It happened to me last week, same RStudio version (RStudio 2022.02.2 Build 485), R.4.2.0, Windows machine.
6 months worth of notes and code snippets that disappeared, but it could have been on more vital projects.
I can't think of any element that could help replicate the issue unfortunately.

I assume you're also on a Windows machine? And it's not quite the same issue, since the post you link to refers to version 4.2. I've not experienced the issue myself, since I've been doing most of my work on Linux systems, recently, and hence had not updated my Windows RStudio for a while. Now that I've updated to Build 485 (still running R version 4.1.2 at this point), I've not managed to reproduce your issue, but spotted some odd behavior on the end of a session. See the following figure:

Immediately upon closing RStudio or quitting the session, an error message appears in the R console, stating that R could not write to ~/.Rhistory because access denied. The error was also logged in C:\Users{Your Username}\AppData\Local\RStudio\log\rsession-{Your Username}.log

There's also the following warning:

WARNING findProgramOnPath returns wrong result: C:\texlive\2021\bin\win32\pdflatex.exe != C:/texlive/2021/bin/win32/pdflatex.exe;

That one's odd because, on a Windows system, it seems to be looking for a path with slashes rather than backslashes.

To me it looks like the newest version of RStudio does not handle Windows paths correctly, since the logged errors show paths with '/' instead of ''. The issue does not seem related to R, since R can perfectly handle such paths, but I don't know about RStudio. Furthermore, this would explain why everyone with this issue appears to be using Windows.

So assuming that the issue is actually related to RStudio, what can you do? I'm afraid not much apart from waiting for a patch. However, since saving seems to work fine on my end, you could try to work with R Projects in the meantime and use version control (git) to ensure that you can quickly restore any lost work.

Edit: Here's the full error message with minor formatting.

2022-05-25T14:51:57.489827Z [rsession-thoma] 
ERROR system error 5 (Zugriff verweigert) [path: C:/Users/thoma/Documents/.Rhistory]; 
OCCURRED AT 
  auto __cdecl rstudio::core::FilePath::openForWrite::
    <lambda_ee6056efa5463874f96f1d4ce6e5cdd4>::operator ()(void) const 
    src/cpp/shared_core/FilePath.cpp:1498; 

LOGGED FROM: 
  void __cdecl rstudio::r::session::reportHistoryAccessError(
    const class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> &, 
    const class rstudio::core::FilePath &, const class rstudio::core::Error &) 
    src/cpp/r/session/RInit.cpp:403

Edit 2: Upgraded to R version 4.2 and still could not reproduce issue. Can you provide any additional details (for example whether your username contains any special characters, or whether certain paths contain whitespace, what you mean by logging off, and so on).

Also, can you try to open your R file in a different editor (even notepad.exe will do) while RStudio is running and after you've saved the file to check whether the code is actually saved to disk?

Thank you for your response.

Indeed I was on a Windows (Win10) machine. In fact I work on Debian distr. in my personal machine and I am given a Windows PC at work. In both machines same distro of R and same packages/pkg versions are installed and so same RStudio version. The issue has come up while using the Win10 PC.

Up to date I have never faced the odd behavior you mention, nor any warning concerning PATH. I always open RStudio as Administrator when working on Windows, to avoid such issues as denied access.
Still I had tried to save some of the lines I had all already run via .Rhistory file and it seemed to have no problem (that is it had stored the latest commands). Running history() though was not as successful.

I can't reproduce the issue on purpose either. It seemed random but I have already faced it 3 times, as I foretold. My user name has no "odd" characters. I use a portable drive to carry my work with me, which has a 'space' character in its default name, but I only work directly on it when I'm on Linux and never had any problem with that.

So, it seems that what is done is done and I have to wait for a patch. Yes, having the R file opened in a different editor is what I am aiming to do.
Thank you.

Hi. I faced the same issue on Windows (both on Windows 7 as well as Windows 10). File content ist still visible in RStudio but in Explorer the file has 0 Byte and no content.
It happens under the following conditions: file size of the content has to be at least 1 KB (estimated) - for smaller files everything works fine,- and file encoding has to be ISO-8859-1.
If you save your source code "Save with encoding ..." using UTF-8 you immediately get your file content back (if you did not close the editor before ...)

1 Like

It seems the developers have taken this de-bugging into account. They claim the issue is fixed in the latest release ( RStudio 2022.02.3+492).
Thank you!

RStudio Release Notes - RStudio
R 4.2: Document unexpectedly opens as empty in some cases · Issue #11206 · rstudio/rstudio · GitHub

@_Martin and @AlexAz as I mentioned on the Github.... a friend of mine reported this issue to me about a month ago. At the time, I believed it was due to one drive syncing as their manager requires everyone to sync their entire user folder(*). This is because the error they got was different--onedrive automatically locks the files into read-only mode and the file wouldn't autosave because of a file permission error. In conclusion, you may still have an issue if the file you are editing is one that is in an onedrive or sharepoint folder.

Thank you for letting me know about this latest patch. I may ask the to see if the issue is fixed for them tomorrow.

(*)Including the R application itself. I suspect it might be painfully obvious every 5 minutes the temporary application directories like the %App Data% folder might lead too.

Thank you all !
Yep, it's fixed now.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.