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.

Rickest Rick
  • 1,519
  • 1
  • 15
  • 28
Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • 3
    there is a hidden folder .Rproj.user . did you have a look at it? – tagoma Feb 07 '16 at 17:45
  • 3
    Where is that hidden folder – Agaz Wani Feb 08 '16 at 03: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. – user5249203 Feb 08 '16 at 19:56
  • close the new project or go back to the previous project, they should be there – rawr Feb 13 '16 at 00: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 '16 at 12:07
  • does your `global options/preferences > general` have a check box next to restore previously open source documents? – rawr Feb 13 '16 at 12:26
  • @rawr That check box is on – Agaz Wani Feb 14 '16 at 08:58

8 Answers8

57

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.

MarkusN
  • 3,051
  • 1
  • 18
  • 26
Iman
  • 2,224
  • 15
  • 35
  • This was life-saving, and probably the best answer for Windows users not using an RProject (shame on me). Thanks! – pedrostrusso Aug 14 '18 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 '19 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... – Alexandre georges Sep 26 '19 at 07: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 :) – FatihSarigol Jun 12 '20 at 16:19
  • 1
    Is there a similar approach in Ubuntu? – Cris Mar 16 '21 at 20:13
  • This approach may have worked at some point but as of March 2022, I navigated to the folder in the path above, and it has a few scripts, which are all 2+ days old, and even then it contains about 25% of the entire thing. Look down at the answer that begins with "If someone is still looking for the answer..." – Adam_S Feb 28 '22 at 21:01
  • Thanks so much! You just saved me hourrsss!!! – DonCarleone Aug 19 '22 at 02:37
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)
Prradep
  • 5,506
  • 5
  • 43
  • 84
  • 3
    Life. Saver. For all those seeking an answer, it is here. – Dan Sep 19 '18 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 '22 at 20:57
23

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.

Divya Manglam
  • 491
  • 2
  • 13
  • 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 08 '16 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. – Divya Manglam Feb 08 '16 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 '22 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.

AR_Domingo
  • 153
  • 1
  • 10
  • 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 '22 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).
Omar Wasow
  • 1,870
  • 24
  • 24
  • 1
    This is very simple, yet a life saving trick. This should be marked as the answer. – Azim Feb 04 '18 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 09 '18 at 07: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.

Tamas Ferenci
  • 424
  • 2
  • 10
  • 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 '22 at 01:08
1

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.

0

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!

Vishal Sharma
  • 289
  • 2
  • 10