Run for loop across multiple cells in ipynb - jupyter-notebook

I have a Jupyter notebook which runs a script in R (not python unfortunately).
In one of the cells of the notebook, I assign a text to variable (e.g. iso <- 'USA'). I'd like to change this, so that this cell is a list of text strings (e.g. iso <- c('USA', 'Canada'...)). Then, I'd like to run every subsequent cell for every item on the list.
How can I do this? Its basically a for loop that runs across multiple cells in the notebook.
Help!

Related

R Markdown outputs disappear some time after creation

I'm working in an R Markdown document - after creating a number of outputs (ggplots, ggmaps, base r plots, kable tables) I then go on to work further down in the document, but after a period of time (maybe 10-20 minutes) I scroll back up to find my visual outputs gone. They are not hidden (using Expand/Collapse Output) but gone entirely, requiring me to rerun the cell to show them again.
This occurs after a period of time either when running indivudial cells or after restarting R and rerunning the entire file.
Is there a setting I'm missing in my notebook or in R? Many thanks

Jupyter notebook, move cells from one notebook into a new notebook

Is it possible to move n cells from one notebook to a new notebook?
Programmatically move n cells from one notebook to a new one
Yes, you can use nbformat to take a notebook and limit the content of the new notebook to the a block of n cells of the original notebook.
"The nbformat package allows you to programmatically read and parse notebook files." - SOURCE, Tony Hirst's description
nbformat comes as part as Jupyter so it runs wherever you have your notebooks running.
Making a notebook from the first n cells of a notebook
I'm going to base this mainly on code adapted from my post at the Jupyter Discourse forum here. I have other examples with nbformat-related code you can get to from links below that post.
You can paste this code in a notebook that's running where your input notebook is located:
number_cells_to_keep = 5 # number of first n cells in the input notebook to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if indx < number_cells_to_keep]
nbf.write(new_ntbk, "first_n_cells_of_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the number on the first line to adjust the number of cells at the top of the original notebook that will be moved to the new notebook.
Making a notebook from a block of cells at a start point & spanning n number of cells
You could adapt the above code to select a specific internal range of cells.
This example illustrates starting the interval of cells at the fifth code cell and taking that and the following cells for a total of ten from the originating notebook to produce the new notebook:
cell_to_start_collecting_at = 5 # number of cell to start the span of cells to collect; first cell gets number 1 in bracket if run so use that numbering
length_of_cell_block_to_keep = 10 # length of sequential span of cells to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if cell_to_start_collecting_at - 2 < indx < (cell_to_start_collecting_at + length_of_cell_block_to_keep - 1)]
nbf.write(new_ntbk, "has_interval_of_cells_from_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the first two lines to adjust the starting cell number and length of the block of cells to move to the new notebook.
The collection of the cells for the new notebook using nbformat while iterating on the original notebook cells with for cell in ntbk.cells or a variant could be adapted & made to be complex. For example, if you had a list of certain cells to go into the new notebook or only wanted to count code cells for the point at which to start.
Use JupyterLab to drag by hand a sequence of cells to a new notebook
If you are trying to move a block of cells from one notebook to a new one using JupyterLab's graphical user interface, you open your new notebook and arrange it side-by-side next to the original notebook in your main pane by dragging the tabs with the notebook names to arrange them.
Then you can select the cells in the original notebook and then dragging from the top of that highlighted block, drag them over into the new notebook.
Save the edited version of the 'new' notebook.
Video illustrating the arranging and dragging approach is here.
It is featured under 'Drag cells between notebooks to quickly copy content' on the Notebooks page in the JupyterLab documentation.

In R, is there any way to automatically run an output after it has been printed?

Sorry if this sounds a bit messy, I'm new to using R. I have a data frame named "AB13" ( a postcode) and I am using a list of all postcodes which I imported as a .csv file. Is there any way I can print one of the columns and run it through the console automatically. Say print the 10th element of the list, which is AB13. Then it will display AB13 ( the data frame)?
I have tried using print, cat and multiple other print functions but I haven't been able to get anywhere.
You can do
get(your_list[[10]])

Browser/viewer to see an R dataset in Azure Notebooks?

I understand that the command line can be used to view data, e.g. head(), tail(), etc.
However, I'd like to view the whole dataset. Is this possible within Jupyter Azure Notebooks?
(Untested) if you have a DataFrame object, calling the name should print the entire dataset.
For example, if your dataframe is named dta, just have
dta
at the end of a cell, and run that cell.

tibble table display issue when using R in jupyter notebook

I just started using R in Jupyter notebook. There seems to be some issue displaying tibble table.
for example,
mtcars
Everything is normal.
If mtcars is converted to tibble,
car<-as_data_frame(mtcars)
car
The table displayed is totally screwed....
Anyone knows why? Do I need to set some options in jupyter notebook?
A follow-up question: How to control the number of rows for table output (not using head())? Is there any notebook options I can set? Is there any way that display the whole table with some page number button like those in R notebook?
Are you sure that you are using the latest version of Jupyter Notebook? I get exactly the same printout for the base data frame and the tibble.
As for the number of rows to show, I don't know of any notebook setting. If you don't want to use head(), you can always do:
mtcars[seq.int(15),]
with whatever number of lines you want instead of 15.

Resources