43

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 anyone help to get my unsaved script tabs.

7
  • 3
    there is a hidden folder .Rproj.user . did you have a look at it?
    – tagoma
    Feb 7, 2016 at 17:45
  • 3
    Where is that hidden folder
    – Agaz Wani
    Feb 8, 2016 at 3:12
  • 1
    How about restoring your OS system back to particular date and then open the .Rproject if you have one saved or find the history file ? ...open .Rhistory in Rstudio. Feb 8, 2016 at 19:56
  • close the new project or go back to the previous project, they should be there
    – rawr
    Feb 13, 2016 at 0:41
  • @rawr I also believe that the unsaved scripts should be there, but unfortunately not found. The behavior you are talking about I also saw previously with Rstudio. But this time i am not sure what went wrong and i am not able to get my unsaved scripts on opening the old project or by doing anything else.
    – Agaz Wani
    Feb 13, 2016 at 12:07

9 Answers 9

59

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.

7
  • This was life-saving, and probably the best answer for Windows users not using an RProject (shame on me). Thanks! Aug 14, 2018 at 19:37
  • OMG thanks. Accidentally mv-ed a file on top of another in git-bash and thought I lost it. You're a lifesaver!
    – awwsmm
    Jul 10, 2019 at 15:00
  • Thank you, you saved me like 3 hours of hard work and @pedrostrusso, I did use a RProj (same files in it with -contents) but closed it... Sep 26, 2019 at 7:34
  • 1
    Hey we can also open those contents files on Rstudio as well directly via Open file to plug them back where they were! Huge Thanks :) Jun 12, 2020 at 16:19
  • 1
    Is there a similar approach in Ubuntu?
    – Cris
    Mar 16, 2021 at 20:13
27

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)
2
  • 3
    Life. Saver. For all those seeking an answer, it is here.
    – Dan
    Sep 19, 2018 at 16:26
  • 1
    THIS. IS. THE. ANSWER. The other suggestions may have worked at one point but for me on 3/1/22 they did not. The answers that begin with "If you are on Windows..." and "I assume you are using R-Studio" had some syntax but nothing newer than 3 or so days. This answer above, this is it right here. Thank you Prradep!!!!
    – Adam_S
    Feb 28, 2022 at 20:57
23
+25

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.

3
  • 2
    Thanks for your comments. 1) .Rhistory has some code, but looks like junk, could not find the files. 2) The behaviour of RStudio I saw previously is different from the current situtation. I used to get all the tabs on opening RStudio, but now case is different. Its opens only that script which I ask RStudio to open.
    – Agaz Wani
    Feb 8, 2016 at 10:11
  • 2
    Try opening the .Rhistory file in RStudio and not a word editor. Notepad or NP++ won't display the file properly. Feb 8, 2016 at 10:32
  • This answer assumes you have either recently saved a workspace or a script. In either case, we would not be here if we'd done that. Answer that begins with "If someone is still looking for..." has the correct way.
    – Adam_S
    Feb 28, 2022 at 20:59
14

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.

1
  • Just used this to find code I ran 3 months ago, the other top solutions which have worked for me in the past failed me here. This is extremely useful if you have a snippet of code that is unique from everything else you have ran that you can search for in notepad.
    – cdtip
    Sep 22, 2022 at 17:08
12

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).
2
  • 1
    This is very simple, yet a life saving trick. This should be marked as the answer.
    – Azim
    Feb 4, 2018 at 18:04
  • 2
    You just saved my life. How come this is not coming up at the top of Google result? And why is RStudio acting so weird to expect users to close the project to retrieve temporarily saved scripts? Life is full of wonders.
    – zebralamy
    Jun 9, 2018 at 7:44
3

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.

1
  • I ran into this exact problem today. After deleting lock_file and restart_file, all unsaved scripts show up again. Thank you!
    – Tung
    Oct 23, 2022 at 1:08
2

Echoing the ideas above for history_database to find a record of all prior code that has been run through the console.

For anyone using RStudio on a Linux-based server: I was able to find this at the following location ~/.local/share/rstudio/history_database.

Hope this helps anyone who loses their saved code and needs to piece it back together.

1

Some of the things you might wanna know are:

  1. Inside C:\Users\[Your-User]\AppData\Local\RStudio-Desktop, there is a history_database which includes all the commands executed in your all the RStudio sessions
  2. For every session, in your working directory, you will notice that there is a hidden folder with the name .Rproj.user.
  • Inside this folder, there is a shared folder and another folder with a key generated. enter image description here

  • This folder is a subset of C:\Users\[Your-User]\AppData\Local\RStudio-Desktop. The difference is that it is for one particular session and the first one is for all the sessions.

  • If you go to the sources folder, you'll notice more folders like below:

enter image description here

  • The folder you're looking for could be inside one of these folders

enter image description here

  • The file name XXXXXXXXX is the metadata about the related unsaved/saved script whereas, XXXXXXXX-contents contains the code of the unsaved/saved script.

  • If you open the metadata, there is a tag called tempName under properties which contains the untitled name of the file.

enter image description here

  • This way you can retrieve the unsaved script you're looking for
  1. There is another file in your working directory i.e., .Rhistory. This file keeps a history of all the commands executed on terminal. Please note that even if you execute a command from the R Script, it runs from the terminal only. Hence, you can also copy your recently executed commands to get back the entire script.

I hope you'll find what you're looking for.

Keep Coding!

-1

For Windows: if you've previously set up a file association so that double-clicking on a .R file opens and sends it to Rstudio, then the following has worked several times for me:

  1. Use the Windows Task Manager to close all RStudio instances.
  2. Now use the Windows File Explorer to navigate to a .R file. Double-click on it. In my experience, this restores all previous saved scripts.

Not the answer you're looking for? Browse other questions tagged or ask your own question.