I would like to know, if it is possible to get unsaved script tabs in R studio. I accidentally open and switch to new project, then my unsaved scripts disappeared. Can anyo
If someone is still looking for the answer or will look in the future, please read the below working approach
As far as I Know, there is no straightforward way to restore the unsaved script tabs in the last session or before the Rstudio crashes. The project folder usually contains the hidden folder .Rproj.user with many sub folders, source database
is one of them (shortly called as sdb). In the sub folders within sdb having the prefix "s-", you can find almost all the unsaved script tabs in JSON format. You can copy and use them to get the contents of the unsaved script tabs.
For example, I have a file named BDFFFF92
is present within the project location .Rproj.user/586F3E74/sdb/s-DB8D414F/
# reading the JSON file
dataObj <- jsonlite::fromJSON('.Rproj.user/586F3E74/sdb/s-DB8D414F/BDFFFF92', simplifyVector = T)
The actual content in the unsaved scripts will be present in contents
of the JSON object.
# Printing the content from the JSON object
writeLines(dataObj$contents)
If you are on Windows, Go to the folder:
C:\Users\[your user]\AppData\Local\RStudio-Desktop\sources
where there are all of your unsaved tabs in folders, especially those that begin with s
like s-******
.
There are two kinds of file (eg. D395C3B4
and D395C3B4-contents
)
D395C3B4
file contains JSON information about your unsaved tab (eg. "tempName" : "Untitled76")
and your correspanding codes are in D395C3B4-contents
file.
Open D395C3B4-contents
with notepad.
I assume that you are using Rstudio. Generally, it stores the entire workspace, including unsaved scripts & console history, as is even if you don't save it.
There are two things that you can do to retrieve the code in those scripts depending upon whether you ran the files or not.
Hope this helps.
RStudio Projects can be confusing at first. In simplest terms, the RStudio project does two things:
setwd()
and generating conflicts with different local paths) What's not obvious is that the default setting in RStudio is to create scripts in "Project: (None)".
So, if you haven't set up any projects previously, to restore your missing scripts you need to return to "Project: (None)" which contains all the open work unaffiliated with any RStudio project.
To do this, go to the upper right hand corner of your RStudio window and do the following:
I ran into a situation when RStudio didn't load any scripts, none of the above suggestions helped, however, I could see all of the files under C:\Users\[your user]\AppData\Local\RStudio-Desktop\sources
.
What helped me was to delete lock_file
and restart_file
. After this, when I started RStudio, all scripts were loaded.
To everyone that couldn't recover their script even with the .Rhistory:
I could recover mine searching in the "AppData" folder, located in your working drive. For my case it was located in "C:\Users\my_user\AppData\Local\RStudio-Desktop". Then there's a file called "history_database" which opening it with the notepad I could see ALL of my previous coding.
If you reopen this file in Rstudio you can see it like a log history. If its too big to see all the entries, just copy it to a notepad and open it again with R (Ctrl + O). I dont know why I didnt see this alternative anywhere else...
I'm just starting in R, so to rearrange again the script I copied the "log-like history" displayed in R to an Excel and then separate them with text-to-columns by a fixed width. This is what worked for me, no other recovery method did.