Skip to content

Instantly share code, notes, and snippets.

@tarunparmar
Last active January 26, 2017 17:16
Show Gist options
  • Save tarunparmar/16113a1d6de5a4aabf2d2741c39b79bc to your computer and use it in GitHub Desktop.
Save tarunparmar/16113a1d6de5a4aabf2d2741c39b79bc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": false
},
"cell_type": "markdown",
"source": "# How to install R kernel for Jupyter ?"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "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.\n\nFirst, 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:"
},
{
"metadata": {
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "$ R\n\n> install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "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."
},
{
"metadata": {
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "> devtools::install_github('IRkernel/IRkernel')",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "If above command doesnt work then you may try it in RStudio. \nThen, you still need to make the R kernel visible for Jupyter:"
},
{
"metadata": {
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "# Install IRKernel for the current user\n\n> IRkernel::installspec()\n\n# Or install IRKernel system-wide\n\n> IRkernel::installspec(user = FALSE)",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "One other thing to do before we open jupyter is to go to location :"
},
{
"metadata": {
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "C:\\Users\\tparmar\\AppData\\Roaming\\jupyter\\kernels\\ir",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now open ``kernel.json`` to modify the path to ``\"C:/Program Files/R/R-3.3.1/bin/x64/R\"``\nPlease make sure your default R package Library folder is on C drive as show above.."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "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. "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You may also checkout Plotly R site to see more examples [Plotly Charts In Jupyter Notebooks Using R](https://plot.ly/r/using-r-in-jupyter-notebooks/)\n"
},
{
"metadata": {
"trusted": false,
"collapsed": false
},
"cell_type": "code",
"source": "getwd()",
"execution_count": 6,
"outputs": [
{
"metadata": {},
"data": {
"text/html": "'C:/Users/tparmar'",
"text/markdown": "'C:/Users/tparmar'",
"text/plain": "[1] \"C:/Users/tparmar\"",
"text/latex": "'C:/Users/tparmar'"
},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": false,
"collapsed": false
},
"cell_type": "code",
"source": ".libPaths()",
"execution_count": 7,
"outputs": [
{
"metadata": {},
"data": {
"text/html": "<ol class=list-inline>\n\t<li>'C:/Users/tparmar/Documents/R/3.3.2'</li>\n\t<li>'C:/Program Files/R/R-3.3.1/library'</li>\n</ol>\n",
"text/markdown": "1. 'C:/Users/tparmar/Documents/R/3.3.2'\n2. 'C:/Program Files/R/R-3.3.1/library'\n\n\n",
"text/plain": "[1] \"C:/Users/tparmar/Documents/R/3.3.2\" \"C:/Program Files/R/R-3.3.1/library\"",
"text/latex": "\\begin{enumerate*}\n\\item 'C:/Users/tparmar/Documents/R/3.3.2'\n\\item 'C:/Program Files/R/R-3.3.1/library'\n\\end{enumerate*}\n"
},
"output_type": "display_data"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Time to run a sample R code with Plotly to make sure we have full R functionality"
},
{
"metadata": {
"trusted": false,
"collapsed": false
},
"cell_type": "code",
"source": "library(ggplot2)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": false,
"collapsed": false
},
"cell_type": "code",
"source": "library(plotly)",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Loading required package: ggplot2\nWarning message:\n\"package 'ggplot2' was built under R version 3.3.2\"\nAttaching package: 'plotly'\n\nThe following object is masked from 'package:ggplot2':\n\n last_plot\n\nThe following object is masked from 'package:stats':\n\n filter\n\nThe following object is masked from 'package:graphics':\n\n layout\n\n",
"name": "stderr"
}
]
},
{
"metadata": {
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "knitr::opts_chunk$set(echo = TRUE)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "source(\"//fc8fss04/tparmar/Profile/Documents/R/myR/Yahoo Stock Data Pull.R\")\n \nMSFT <- GetYahooData(\"MSFT\")\nINTC <- GetYahooData(\"INTC\")\nIBM <- GetYahooData(\"IBM\")\nAAPL <- GetYahooData(\"AAPL\")\n\nmat <- data.frame(Date = MSFT$Date, \n MSFT = round(MSFT$Adj.Close,2),\n IBM = round(IBM$Adj.Close,2),\n AAPL = round(AAPL$Adj.Close,2),\n INTC = round(INTC$Adj.Close,2))\n \n\nsummary(mat)",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "[1] \"Data pull successful...\"\n[1] \"Data pull successful...\"\n[1] \"Data pull successful...\"\n[1] \"Data pull successful...\"\n",
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"text/plain": " Date MSFT IBM AAPL \n 2010-02-01: 1 Min. :19.29 Min. :103.8 Min. : 24.99 \n 2010-02-02: 1 1st Qu.:23.99 1st Qu.:142.2 1st Qu.: 51.12 \n 2010-02-03: 1 Median :30.60 Median :158.1 Median : 73.92 \n 2010-02-04: 1 Mean :34.67 Mean :154.1 Mean : 76.25 \n 2010-02-05: 1 3rd Qu.:44.11 3rd Qu.:171.7 3rd Qu.:102.92 \n 2010-02-08: 1 Max. :63.68 Max. :193.6 Max. :128.52 \n (Other) :1753 \n INTC \n Min. :14.33 \n 1st Qu.:18.64 \n Median :22.53 \n Mean :24.35 \n 3rd Qu.:30.80 \n Max. :37.81 \n "
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "library(plotly)\nlibrary(dplyr)\np <- plot_ly(mat, x = ~Date, y = ~MSFT, type = \"scatter\", mode = \"lines\", fill = \"tozeroy\", name = \"Microsoft\") %>% \n add_trace(y = ~INTC, fill = \"tonexty\", name = \"Intel\") %>% \n add_trace(y = ~AAPL, fill = \"tonexty\", name = \"Apple\") %>% \n add_trace(y = ~IBM, fill = \"tonexty\", name = \"IBM\") %>% \n layout(title = \"Stock Prices\", \n xaxis = list(title = \"Time\"),\n yaxis = list(title = \"Stock Prices\"))\np # Thats it !",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "Loading required package: ggplot2\nWarning message:\n\"package 'ggplot2' was built under R version 3.3.2\"\nAttaching package: 'plotly'\n\nThe following object is masked from 'package:ggplot2':\n\n last_plot\n\nThe following object is masked from 'package:stats':\n\n filter\n\nThe following object is masked from 'package:graphics':\n\n layout\n\nWarning message:\n\"package 'dplyr' was built under R version 3.3.2\"\nAttaching package: 'dplyr'\n\nThe following objects are masked from 'package:stats':\n\n filter, lag\n\nThe following objects are masked from 'package:base':\n\n intersect, setdiff, setequal, union\n\nWarning message in arrange_impl(.data, dots):\n\"'.Random.seed' is not an integer vector but of type 'NULL', so ignored\"",
"name": "stderr"
},
{
"output_type": "display_data",
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment