How to install R kernel for Jupyter ?

To work with R, you’ll need to load the IRKernel and activate it to get started on working with R in the notebook environment.

First, you'll need to install some packages. Make sure that you don't do this in your RStudio console, but in a regular R terminal:

In [ ]:
$ R

> install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))

This command will prompt you to type in a number to select a CRAN mirror to install the necessary packages. Enter a number and the installation will continue.

In [ ]:
> devtools::install_github('IRkernel/IRkernel')

If above command doesnt work then you may try it in RStudio. Then, you still need to make the R kernel visible for Jupyter:

In [ ]:
# Install IRKernel for the current user

> IRkernel::installspec()

# Or install IRKernel system-wide

> IRkernel::installspec(user = FALSE)

One other thing to do before we open jupyter is to go to location :

In [ ]:
C:\Users\tparmar\AppData\Roaming\jupyter\kernels\ir

Now open kernel.json to modify the path to "C:/Program Files/R/R-3.3.1/bin/x64/R" Please make sure your default R package Library folder is on C drive as show above..

We are done, open up the notebook application with jupyter notebook. You'll see R appearing in the list of kernels when you create a new notebook.

You may also checkout Plotly R site to see more examples Plotly Charts In Jupyter Notebooks Using R

In [1]:
getwd()
'C:/Users/tparmar/Python'
In [2]:
.libPaths()
  1. 'C:/Users/tparmar/Documents/R/3.3.2'
  2. 'C:/Program Files/R/R-3.3.2/library'

Time to run a sample R code with Plotly to make sure we have full R functionality

In [3]:
library(ggplot2)
In [8]:
library(plotly)
Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout

In [9]:
knitr::opts_chunk$set(echo = TRUE)
In [10]:
source("//fc8fss04/tparmar/Profile/Documents/R/myR/Yahoo Stock Data Pull.R")
 
MSFT <-  GetYahooData("MSFT")
INTC <- GetYahooData("INTC")
IBM <- GetYahooData("IBM")
AAPL <- GetYahooData("AAPL")

mat <-  data.frame(Date = MSFT$Date, 
                   MSFT = round(MSFT$Adj.Close,2),
                   IBM = round(IBM$Adj.Close,2),
                   AAPL = round(AAPL$Adj.Close,2),
                   INTC = round(INTC$Adj.Close,2))
 

summary(mat)
[1] "Data pull successful..."
[1] "Data pull successful..."
[1] "Data pull successful..."
[1] "Data pull successful..."
         Date          MSFT            IBM             AAPL       
 2014-02-03:  1   Min.   :32.84   Min.   :113.8   Min.   : 67.20  
 2014-02-04:  1   1st Qu.:41.30   1st Qu.:144.3   1st Qu.: 94.36  
 2014-02-05:  1   Median :45.16   Median :152.2   Median :106.67  
 2014-02-06:  1   Mean   :47.20   Mean   :153.7   Mean   :104.02  
 2014-02-07:  1   3rd Qu.:52.84   3rd Qu.:166.2   3rd Qu.:115.21  
 2014-02-10:  1   Max.   :65.38   Max.   :181.7   Max.   :135.72  
 (Other)   :762                                                   
      INTC      
 Min.   :21.48  
 1st Qu.:28.64  
 Median :31.16  
 Mean   :30.83  
 3rd Qu.:33.63  
 Max.   :37.71  
                
In [16]:
library(plotly)
library(dplyr)
p <- plot_ly(mat, x = ~Date, y = ~MSFT, type = "scatter", mode = "lines", fill = "tozeroy", name = "Microsoft") %>% 
  add_trace(y = ~INTC, fill = "tonexty", name = "Intel") %>% 
  add_trace(y = ~AAPL, fill = "tonexty", name = "Apple") %>% 
  add_trace(y = ~IBM, fill = "tonexty", name = "IBM") %>% 
  layout(title = "Stock Prices", 
         xaxis = list(title = "Time"),
         yaxis = list(title = "Stock Prices"))
p  # Thats it !