{ "metadata": { "kernelspec": { "codemirror_mode": "R", "display_name": "R", "language": "R", "name": "ir" }, "name": "", "signature": "sha256:769b53de9bc90dc3041db26edc113bb1a72abdd9c76947f292e60f3eefa53f70" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic operations" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"Hello world! Love, R in IPython.\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1:78\n", "a ^ 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting works too:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = seq(0, 2*pi, length.out=50)\n", "plot(x, sin(x))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "No tab completion yet - come and implement it!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rich display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a separate `IRdisplay` package with the display API - it will be possible to use the same function calls with `%%R` magics from the IPython kernel.\n", "\n", "The plotting example above uses `display_png`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "library(IRdisplay)\n", "\n", "display_html('Repo: takluyver/IRkernel
')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is already seeing serious use. Here's a notebook by [Ramnath Vaidyanathan](https://github.com/ramnathv/):" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Leaflet Routing Machine Plugin\n", "\n", "I saw the [following issue](https://github.com/ramnathv/rMaps/issues/45) posted to the [rMaps](http://github.com/ramnathv/rMaps) github repo today.\n", "\n", "> I am new to using rMaps and leaflet. I would like to plot the route between two locations. The leaflet routing machine plugin allows us to do this (https://github.com/perliedman/leaflet-routing-machine). I am not quite sure how to use the functions `addAssets()` and `setTemplate()` to be able to use this plugin.\n", "\n", "This was a good exercise for me to test whether these newly introduced mechanisms `addAssets` and `setTemplate` would allow one to easily extend the base leaflet binding in [rMaps](http://rmaps.github.io).\n", "\n", "Let us start by creating the base map." ] }, { "cell_type": "code", "collapsed": false, "input": [ "library(rMaps)\n", "map = Leaflet$new()\n", "map$setView(c(40.73846, -73.99413), 16)\n", "map$tileLayer(provider = 'Stamen.TonerLite')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "display_chart = function(viz){\n", " y = paste(capture.output(viz$show('iframesrc', cdn = TRUE)), collapse = \"\\n\")\n", " IRdisplay::display_html(y)\n", "}" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "display_chart(map)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now want to add a route between the following waypoints. I have chosen the data structure to be an unnamed list of vectors, since it converts easily to the JSON structure expected by by routing plugin." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mywaypoints = list(c(40.74119, -73.9925), c(40.73573, -73.99302))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to use the routing plugin, we first need to add the required js/css assets. I introduced the `addAssets` method in the `dev` version of `rCharts` precisely to serve this need (NOTE: It is currently a little buggy in terms of order in which the assets are specified, but I will take care of that this week)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "map$addAssets(\n", " css = \"http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css\",\n", " jshead = \"http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.min.js\"\n", ")\n", "\n", "routingTemplate = \"\n", " \"\n", "\n", "map$setTemplate(\n", " afterScript = sprintf(routingTemplate, RJSONIO::toJSON(mywaypoints))\n", ")\n", "display_chart(map)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Original notebook on nbviewer](http://nbviewer.ipython.org/urls/gist.githubusercontent.com/ramnathv/9998388/raw/349d6c6756679610cbb78812911f6b15da56e12f/rmaps_leaflet_routing.ipynb?create=1)" ] } ], "metadata": {} } ] }