How to get unsaved script tabs

后端 未结 6 2143
渐次进展
渐次进展 2020-12-13 19:32

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

相关标签:
6条回答
  • 2020-12-13 19:51

    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)
    
    0 讨论(0)
  • 2020-12-13 19:57

    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.

    0 讨论(0)
  • 2020-12-13 19: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.

    0 讨论(0)
  • 2020-12-13 20:00

    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).
    0 讨论(0)
  • 2020-12-13 20:02

    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.

    0 讨论(0)
  • 2020-12-13 20:11

    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.

    0 讨论(0)
提交回复
热议问题