Running code cells sequentially in google colab - jupyter-notebook

I want to run the code cells in Google Colab in sequence.
For example:
Cell[1]
from catboost import CatBoostRegressor
#do something here
Cell[2]
clf = CatBoostRegressor(task_type='GPU')
#do some more things here
But when I select "Run all" all cells seem to run in parallel, so my code does not work.
When I do the same thing in a Kaggle Kernel it runs perfectly i.e, first cell[1] is executed, and then cell[2] and so on.
I have tried searching for this in Google Colab but failed to come up with an answer.

To run all cells at a time just press ctrl+F9 or go to runtime menu on top and click run all option and all cells run at a time.
If you want to run one by one then just go to runtime menu on top and click run after option and all cells run one by one. Note one thing that if you want to run from top cell then place cursor on top cell and then start.

Run all does indeed run cells sequentially, e.g.,
Can you share a self-contained notebook that reproduces the problem you observe? I suspect the issue is something other than sequential execution.

Related

Switched my running cell to markdown during code run

I have been running a model for the past 24h (optimization of Neural Network parameters by Gaussian process). Tonight, haven't waked up properly form, I hand accidentally switched the cell to markdown and the layout disappeared. Anyway, the code is still running but I don't if the rest of the cells would execute correctly. I like you to tell me if the remaining cells would run as attended or if I should stop and start the whole notebook over again?
this morning the process was over and the code ran perfectly well. So, switching the jupyter notebook cell from code to markdown does not affect the remaining cells and their execution.

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.

How to Run a Single Line in RStudio

I am used to using the standard R GUI but I'm trying to make the switch to RStudio. However, it's driving me nuts that I can't figure out how to run a single line with a keyboard shortcut. For instance, if I have the two lines,
c <- a +
b
I would like to press ^R to run the first line and then have to press ^R again to run the second line to complete the assignment. This is the default setting in the standard R GUI. Unfortunately, RStudio only seems to want to execute a statement to it's completion. Is there any way around this?
If you want this to be the default behavior, go to "Tools -> Global Options -> Code -> Editing" and uncheck the box "Execute all lines in a statement" under "Execution".
Select the first line (by clicking on it three times), then Ctrl-Enter will run the selection, not the statement.

keyboard shortcut for step out while debugging in rstudio

When I'm debugging, every time, I make a change to the source code, I find myself doing the following since it says, "Debug location is approximate because the source is not available.". Is this 3-step process necessary? Is there an easier way?
> debugSource('~/Desktop/foo1.R')
> debug(myfun1)
> myfun1()
Not sure that this is, what you want, but since you use the tag rstudio, it might help.
Clicking in the left border of the script window in RStudio (next to the line numbers) creates a break point. It is marked by a red circle (it it's inside a function) or a red dot (otherwise). If you now click the source button in RStudio, debugSource is run. If the program encounters a breakpoint, it stops and you are able to examine the state of the program. A breakpoint inside a function stops the code when the function is run (and not when it is sourced). The breakpoint will also work when you run the function from the console as in your example.
The breakpoint remains intact when you change your code. So you have, of course, to source your code after every change, but you don't need to rerun debug every time.
For more information on debugging with RStudio, the following link might be helpful: https://support.rstudio.com/hc/en-us/articles/200713843-Debugging-with-RStudio

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.

Resources