Learning Jupyter 5
上QQ阅读APP看书,第一时间看更新

Basic R in Jupyter

Start a new R Notebook and call it R Basics. We can enter a small script just so we can see how the steps progress for R script. Enter the following into separate cells of your Notebook:

myString <- "Hello, World!" 
print (myString)

From here, you will end up with a starting screen that looks like this:

We should note the aspects of the R Notebook view:

  • We have the R logo in the upper-right corner. You will see this logo running in other R installations.
  • There is also the peculiar R O just below the R icon. If the O unfilled circle displays, the unfilled circle indicates that the kernel is at rest, and the filled circle indicates that the kernel is working.
  •  The rest of the menu items are the same as the ones we saw previously.

This is a very simple script–set a variable in one cell and then print out its value in another cell. Once executed (Cell | Run All), you will see your results:

So, just as if you run the script in an R interpreter, you get your output (with the numerical prefix). Jupyter has counted the statements so that we have incremental numbering of the cells. Jupyter has not done anything special to print out variables for debugging; you will have to do that separately.

If we look at the R server-logging statements (a command-line window was created when we started Jupyter), we will be able to see the actions that took place:

$ jupyter notebook 
[I 11:00:06.965 NotebookApp] Serving notebooks from local directory: /Users/dtoomey/miniconda3/bin 
[I 11:00:06.965 NotebookApp] 0 active kernels  
[I 11:00:06.965 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/ 
[I 11:00:06.965 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). 
[I 11:00:17.447 NotebookApp] Creating new notebook in  
[I 11:00:18.199 NotebookApp] Kernel started: 518308da-460a-4eb9-9959-1411e31dec69 
[1] "Got unhandled msg_type:" "comm_open"               
[I 11:02:18.160 NotebookApp] Saving file at /Untitled.ipynb 
[I 11:08:27.340 NotebookApp] Saving file at /R Basics.ipynb 
[1] "Got unhandled msg_type:" "comm_open"               
[I 11:14:45.204 NotebookApp] Saving file at /R Basics.ipynb 

We started the server, created a new Notebook, and saved it as R Basics. If we open the IPYNB file on disk (using a text editor), we will be able to see the following:

{ 
  "cells": [ 
    ...<similar to previously displayed> 
  ], 
  "metadata": { 
    "kernelspec": { 
      "display_name": "R", 
      "language": "R", 
      "name": "ir" 
    }, 
    "language_info": { 
      "codemirror_mode": "r", 
      "file_extension": ".r", 
      "mimetype": "text/x-r-source", 
      "name": "R", 
      "pygments_lexer": "r", 
      "version": "3.4.3" 
    } 
  }, 
  ...<omitted> 
} 

This is a little different than what we saw in the prior chapter on Python Notebook coding. In particular, the metadata clearly targets the script cells to be R script. Note that the actual cells are not specific to a language – they are just scripts that will be executed as per the metadata directives.