How to recover deleted Jupyter notebook cell? - jupyter-notebook

I use Jupyter notebook. I accidentally deleted a cell, and I can't go step back.
How can I recover the notebook cell?

If you go to "Edit", there's an option for "Undo Delete Cells".
In MacOS
If you are familiar with shortcuts, you can do cmd + shift + p and then type in undo to recover as well. No need to moving your mouse around.
In Windows
First, switch to command mode by pressing ESC, then you can press Z to recover the deleted cells. (Thanks to #nik7 who proposed this answer)

First, switch to command mode by pressing ESC, then you can press Z to recover the deleted cells. It works even in Jupyter Lab which doesn't have "Undo Delete Cells" option under "Edit" tab.

If the notebook kernel is still running and you executed the cell, you can find the content of all cells, including the deleted ones in _ih. For example, the last five cells:
_ih[-5:]

If you need to undo something deleted inside a cell, press:CTRL/CMD + Z
If you need to recover an entire deleted cell hit:
ESC + Z.
Especially this second tip might be useful if you delete a lot of cells by mistake.

if you've already restarted your kernel then %history or _ih[:n] would be empty. You can hope for a recent checkpoint to revert to. You can do this with File > Revert to Checkpoint.
If you want to be sure what's in the checkpoint before you revert - in your working directory (the one your notebook lives in) you'll have a hidden folder that stores your checkpoints. cd .ipynb_checkpoints will get you there. You can look inside the file and see if your lost code is there.
This has just saved me. Found this in an answer here

First, do "ESC" hot key, then you can press Z to recover the deleted cells.
Jupyter Lab work well.

Related

RStudio - How can I have a 3 columns layout?

I'm desperately trying to have as default a 3 columns layout in RStudio and can't manage to obtain it.
I tried to find some documentation, but found nothing with some details or explanation on the "panes" parameters of the rstudio-prefs.json file.
In "C:\Users...\AppData\Roaming\RStudio\rstudio-prefs.json", I saw that it is possible to add a source column using "additional_source_columns": 1 but it is automatically set back to 0 every time I restart R and the problem is that it creates an untitled script in addition to the one I'm opening.
Here are the steps I have to perform to obtain the final layout I'm looking for.
Open a script by double clicking on it
Press CTRL + F7 to add a source column
Drag and drop my script to the left column
remove the 'untitled' script
Thank you for your help ! A link to any documentation that you may know of that I didn't find would be life-saving.
Best
As of today, you can't have a 3 column setup as the default. It's an open Issue on GitHub, marked as an enhancement and added to the "Later" milestone with no due date.
However, with version 2022.02.0+443 (February 2022) of RStudio you can do the following:
Uncheck the option "Restore last opened documents on startup" in Global Options. This will have RStudio open project with no source panes at all.
Using the new option "Open file in Source Column" to open your first file in a new column. This will give you the three columns layout you're looking for.
I would add the "Open file in new column" as a keyboard shortcut, for example CTRL + O. Your workflow would then be:
Open RStudio
CTRL + O to open your first script
Open more scripts any way you want - they will be added to the first column.

Why do you have to rerun the previous code box in Jupyter notebook?

I started using Jupyter notebook today. I used to only use python or just a text editor.
I'm a bit confused about how the workflow works. Sometimes when I compute some variable x in, say, code box 1 and then I use it in code box 2, if I execute code box 1 first and then 2, it'll work. Sometimes when I execute code box 2 by itself, it'll say x is undefined, but shouldn't it store the result from a previous run?
Jupyter's kernel acts like a new console. Forget the order of the cells as they are displayed. That is irrelevant, unless your are hitting "Run all", in which case the notebook decides to run the cells in the up to down order. When you use shift-enter to run an individual cell, you are choosing the order they are being run. Restart the kernel if you want to start fresh.

"/content?title=....htm not found" error message

I'm trying to view my data in RStudio and I get the above strange message, what is that and how do I fix whatever problem there is? I can still work with the data, I just can't see it.
I haven't done anything special, it just "happened". I just keep trying View(df)... and now all of a sudden my data frame is an endless column of 1's, although the View window states, true enough, that df is "30 observations of 2 variables".
I also had this problem several times. For me it always helped to quit and restart RStudio. You might try this.
EDIT:
Normally when quitting RStudio without saving your workspace manually before, you should get a prompt asking you
Save workspace image to $pathToYourWorkspace ?
If this is not the case maybe u have no writing permission in your selected workspace folder. Be sure about that first. If you have the permission you could try saving your workspace manually by entering
save.image('$pathToYourWorkspace/.RData')
Can you try accessing your previous entered commands in the console by using arrow up key? Does it work?
Do you have a file called .Rhistory in your workspace?
If not check Options if u have it activated (Tools -> Global Options -> General) and then check 'Restore .RData into workspace at startup' and 'Always save history ...'
You could also check if u have the latest Version of RStudio installed.

Is there a hack to be able to run the current line or selection in RStudio without moving the cursor?

UPDATE (April 2013): As per answer below, RStudio no longer jumps cursor on selection.
I'm running RStudio 0.97.168.
I like to use the script editor in RStudio like a console. Thus, I run a line of code and then edit it a little bit and re-run it. I often also explore objects by selecting some of the code and running the selection and then progressively altering the selection. At present RStudio always moves the cursor after running a line of code. The cursor can move to a variety of places. Typically the cursor moves to the next line of R code, but depending on the context, it could move to the end of the code block or the next line. It's really frustrating having to constantly move the cursor back to where I want it.
While I often appreciate the default cursor movement behaviour, I'd like to have the option to run the selection or the current line without the cursor moving.
I've raised this as a suggestion on RStudio support.
I'd like to be able to have a shortcut key like "Cmd+Alt+Enter" that runs the current line or selection and does not move the cursor in the script editor.
I realise that this is not currently supported, but I was wondering whether there might be some creative hack that could enable the cursor not to move after running a command or even a patch or perhaps some sort of external macro.
For anyone who ends up here in 2020:
Ctrl(or Cmd) + Enter: Will run current line and jump to the next one. If a code portion is selected, run the selected code without jumping further.
Alt + Enter: – Will run the current line of code without moving the cursor to the next line, useful if you want to run it multiple times.
(Source)
For this kind of flexibility, I suggest you use the editor Sublime Text 2, add in the package installer by Will Bond and then install the SublimeREPL package which will allow you to use an R interpreter within ST2 (or BASH prompt, Python / Ruby / whatever interpeter, concurrently if you wish).
You can then alternate between your code and the interpreter without lifting your fingers from the keyboard and your cursor will be at the same point every time when you want to switch back.
Sublime Text will also allow you to write a custom keybinding to automate this task.
I cannot recommend using Sublime Text 2 highly enough when coding for R. You can even pass files directly from ST2 into RStudio very easily if you like using the plot panes (very easy to do with the SidebarEnhancements package in ST2).
RStudio is awesome for many things -- especially now with Knitr, builds etc etc. But ST2 with an R REPL is many orders of magnitude more powerful for general code writing / editing than RStudio.
Sorry it's not RStudio specific, but it is a nice workaround!
I updated to version 0.98.83 of RStudio using the daily build section.
It appears that at some point in recent versions of RStudio, the cursor no longer jumps when code is run from a selection in the script window.
That's great news.

R unix terminal pager

I'm using R on OS X 10.6 and I prefer the unix console to the R.app for my work. If I issue a help command eg ?print the help content opens through a pager (i tried most and less), which however then hides out the content if I exit it bringing me back to the R input line.
What I really want is that the pager output stays on the screen even after I exit it back to R (hitting q).
I get this desired behaviour on other Readline-based Cli like psql for example, but not on R. Any hints on how this gets configured would be greatly appreciated.
The console pager that R uses can be set with the options function. With less, the -X option suppresses the terminal clearing at exit. So, if the less binary is located in "/bin" (not sure where it's located in OS X), this should work:
> options(pager="/bin/less -X")
If you want this to be the default behavior every time you start R, you can place the above command in your ~/.Rprofile file, which is run automatically at startup.
Alternatively, you can set a export LESS=-XF in ~/.bashrc and this will have an effect across all programs that use less as a pager. The F option would further exit straight away if the content is less than a page, which I find quite useful.

Resources