Welcome to Writing and Running Code in Jupyter Notebook.
After watching this video, you will be able to describe the basic features of
Jupyter Notebook and the advantages of using it to write your R code.
In the following lab, you will practice what you have learned so far using Jupyter
Notebook.
A Jupyter Notebook is made up of a series of cells that you can use to write, run,
and interact with your code.
A cell in a Jupyter Notebook is a multiline text
input field where you can enter many types of text, including programming code,
markdown files,
or raw text that you can later convert into other formats.
Lines, depends on their type, can be executed with different outcomes.
If the lines are code, its execution results are called cell output.
For example, you can print the character string
“Hello Jupyter” and it will be shown in the cell output below the cell.
If you execute cells in a specific sequence,
the cells can access all objects and outputs generated in all previous cells.
For example, you can define a variable “x” cell 1 with a value of 1 and execute it
first,
and then you can access variable x in cell 3.
When creating a new Jupyter Notebook, you must specify
its kernel and then the Notebook keeps one interactive session connected to that
kernel.
Through this interactive session, you can send your code to the kernel to be
executed
and get results back from the kernel.
In this course, the kernel will always be R, but Jupyter Notebook supports many
popular kernels,
such as R, Python, and Julia, covering almost all popular programming languages.
One of the main benefits of Jupyter Notebook is that it contains
almost everything you might need for a coding task or experiment.
For example, it includes:
Narration, which you can use to provide instructions or context to the cells that
follow;
Code, which is the actual logical implementation of your task;
The data that needs to be processed in a task;
And any plots, images, or videos that provide visual insights about the data.
This all-in-one style makes Notebook sharing and reproduction extremely easy.
Also, including narration in a Notebook makes it a great tool
for teaching and learning because instructors can provide detailed,
step-by-step instructions along with code and data for learners, all in one place.
Another advantage of Jupyter Notebook is that it enables you to interact with your
code and data.
For example, you could explore your data from different angles using different
cells.
You can jump back and forth among the cells
to update your code while using other cells’ output as context.
For example, in one cell, you could load some data related to COVID cases
in Canada from a csv file and print a snippet of code with the column names.
Then, by looking at the column names, you may want to return
the max value of confirmed cases, called “numconf”, in the next cell.
By keeping the max confirmed cases in the previous cell output,
you can now get its mean or min value and then compare it with the max value.
A Jupyter Notebook can be easily converted into other popular document formats,
such as pdf,
html, or latex, which makes it very convenient to present or demo your code and
experiment results.
In this video, you learned that a Jupyter Notebook
is made up of cells that can contain code, markdown files, or raw text.
And an all-in-one Notebook contains narration, code, data, and plots, images, or
videos.
You also learned that each Notebook maintains an interactive session with a kernel,
like R, that enables you to interact with its code and data.