Guide for running R with Jupyter Notebook on CoCalc

Almost all tutorials for Jupyter Notebook talk about Python, and spend significant portion of their contents on installation, neither of which is relevant to this class.  Therefore I am writing this short guide for QSBR II students on running R with Jupyter notebook on CoCalc. 

CoCalc runs on the cloud, so that everything has already been set up and ready to go. You don't have to install any software on your own computer and you can access it through a browser anywhere with internet connection, you can even use it with your iPad. Once you login to the CoCalc website, remember that you should use the buttons on the webpage to navigate within the website (blue frame in the figure below). Note that the controls of the browser itself (shown in the read frame in the figure below) do not work for that. 




I. Create a new Jupyter notebook in CoCalc

First, let us create a new Jupyter notebook, give it a name, and it will be automatically saved on CoCalc. As shown in the figures below, first, click on the new buttons (see red arrow), and in the next screen, enter the name (I entered "example" in this case but it could be any name of your choice) and click on "Jupyter notebook". If you click on the "File" button, you will see the file "example.ipynb" has already been created.






II. Choose R as the "kernel" of a Jupyter notebook

As I mentioned earlier, Jupyter notebook was originally created for Python, hence the extension .ipynb. However, it has been extended to accommodate other programming languages, including R. When you open a new Jupyter notebook in CoCalc, depending on your previous setting, it may ask you to select a Kernel. That is, which programming language you will be using for this Jupyter notebook. If you see the page as shown below, select "R(system-wide)", where the red arrow points to. On the upper right corner, where the pink arrow points to, is the current kernel for the notebook. You can change the kernel of this notebook at any time by clinking there.


III.  Write and execute R code in a Jupyter notebook.

I will only discuss the most basic operations of Jupyter notebook necessary for the purpose of our course, Honestly I don't know all the features and there is no need to know all the features. 

The basic unit of a Jupyter notebook is a cell. Click on a cell and its frame turns green, write your R code inside the box, press Enter(Return) key to move to the next line inside the cell. Press Shift + Enter keys together to execute all the code inside the box.  The results will show up below the box with your R script, but they are in the same cell. Jupyter notebook will automatically add a new cell below, in which you can enter your next lines of R code. You can always go back to your previous code, make a change, and run the code again. Note that there is a counter in front of each cell, showing the order of their execution, the counter will keep increase unless you stop the kernel, a.k.a quit the current R session, and restart a new one. One way to restart the kernel is to click on the button pointed by the red arrow in the figure below.  




 




IV.  Write notes in a Jupyter notebook.

You can also write notes (instead of R code) in a cell of Jupyter notebook. By clicking on the button pointed by the blue arrow in the above figure, you will see a pull down menu as shown above. Choose "Raw" for plain text, or "Markdown" if you would like to format the text with Markdown code. You can find more information of Markdown at https://www.markdownguide.org. Similar to executing R-code, press Shift+Enter to process the Markdown code,  and the cell will show the text in the format specified by the markdown code. Unlike R-code, there will be no counters for markdown cells.  

 


Comments