How to get unsaved script tabs

If you are on Windows, Go to the folder:

C:\Users\[your user]\AppData\Local\RStudio-Desktop\sources

update: since RStudio 1.4.* these sources were moved to:

C:\Users\[your user]\AppData\Local\RStudio\sources

the rest of the explanation remains the same.

and if you are on UBUNTU:

home/[your user]/.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.

  • If you've executed the scripts in console, your code would be present in the the console history saved as .Rhistory file which is present in the home directory for R console. The home directory is either the 'Documents' folder or the project folder which was open before.
  • Another thing that you can try is to open a saved script, if any, that was open at the time you switched to the new project. That would open the workspace that was active prior to switching to the new project. Your files should be there.

Hope this helps.


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)

RStudio Projects can be confusing at first. In simplest terms, the RStudio project does two things:

  1. open to a working directory that can operate across different users and computers (e.g., no starting with setwd() and generating conflicts with different local paths)
  2. create a workspace in RStudio with R files, data, etc.

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:

  1. Find the small pull-down menu next to the word Project.
  2. Click on the the small triangle to get the Project pull-down menu and select "Close Project".
  3. Once the new project is closed, your old tabs should reappear. The pull-down menu should also now say: "Project: (None)" (again, this assumes you hadn't set up a project for your prior work).