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.
9 Answers
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.
-
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!– awwsmmJul 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
-
1Hey we can also open those
contents
files on Rstudio as well directly viaOpen file
to plug them back where they were! Huge Thanks :) Jun 12, 2020 at 16:19 -
1
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)
-
3
-
1THIS. 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_SFeb 28, 2022 at 20:57
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.
-
2Thanks for your comments.
1)
.Rhistory
has some code, but looks like junk, could not find the files.2)
The behaviour ofRStudio
I saw previously is different from the current situtation. I used to get all the tabs on openingRStudio
, but now case is different. Its opens only that script which I askRStudio
to open. Feb 8, 2016 at 10:11 -
2Try 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_SFeb 28, 2022 at 20:59
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.
-
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.– cdtipSep 22, 2022 at 17:08
RStudio Projects can be confusing at first. In simplest terms, the RStudio project does two things:
- 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) - 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:
- Find the small pull-down menu next to the word Project.
- Click on the the small triangle to get the Project pull-down menu and select "Close Project".
- 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).
-
1This is very simple, yet a life saving trick. This should be marked as the answer.– AzimFeb 4, 2018 at 18:04
-
2You 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. Jun 9, 2018 at 7:44
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.
-
I ran into this exact problem today. After deleting
lock_file
andrestart_file
, all unsaved scripts show up again. Thank you!– TungOct 23, 2022 at 1:08
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.
Some of the things you might wanna know are:
- 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 - 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.
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:
- The folder you're looking for could be inside one of these folders
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
underproperties
which contains the untitled name of the file.
- This way you can retrieve the unsaved script you're looking for
- 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!
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:
- Use the Windows Task Manager to close all RStudio instances.
- 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.
.Rproject
if you have one saved or find the history file ? ...open.Rhistory
in Rstudio.