There is an, admittedly, not very thoroughly explained answer here, but it did work for me several times when I got this error.
When it happened to me, I quit the R session (since there was no other option), but I did save my script temporarily in another file (simply copy-pasting it).
When I reopened the project and the associated script files, I did not need to use the backup script to update my work --- however, I must say I would always make a backup.
So, in brief, RStudio should be able to restore your script without loss, at least if you reopen the session immediately, but, regretfully, I cannot tell you why, and you should make a backup whenever possible.