Skip to content

Commit

Permalink
Rewrite IHaskell tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
mboes committed Aug 14, 2015
1 parent 9b46f0d commit b38c167
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 1 deletion.
1 change: 0 additions & 1 deletion IHaskell/examples/HelloWorld.ipynb

This file was deleted.

246 changes: 246 additions & 0 deletions IHaskell/examples/tutorial-ihaskell-inline-r.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming R from Haskell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook demonstrates integrating both R and Haskell in the same notebook, using `IHaskell-inline-r`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prelude"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, a bit of setup. We need to enable quasiquotation, to embed R code into Haskell expressions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
":ext QuasiQuotes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we need to make sure that R is initialized properly:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import qualified H.Prelude as H\n",
"initializeEmbeddedR defaultConfig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## R computations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, computations in code cells are interpreted as Haskell code. For instance, here is a definition of the factorial function, in Haskell:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fact 0 = 1\n",
"fact n = n * fact (n - 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a Haskell expression calling `fact`, together with its value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fact 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`inline-r` allows you to embed R expressions and R statements anywhere in Haskell code, using quasiquotation. The following is an IO action that asks R to print the value of the R code snipped embedded between the brackets:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[rprint| 1 + 1 |]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can define the factorial function using R code, just as you can using Haskell code, so long as the R code is delineated within a quasiquote:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[r| fact <- function(n) if(n == 0) 1 else n * fact(n - 1) |]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `r` quasiquote is used for embedding R code that is only useful for its side effects. This is the case with the code above which has the side effect of binding `fact` in the toplevel environment. Applying the definition:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[rprint| fact(10) |]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## R graphics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"R has extremely powerful plotting facilities. They are available out-of-the-box:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[rgraph| plot(cars) |]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For effect, we can fit a straight line through our data set:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[rgraph| plot(cars); abline(lm(cars$dist ~ cars$speed), col=\"red\") |]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"R code snippets that have graphical output should be embedded using the `rgraph` quasiquote. The other quasiquotes ignore graphical output. For a more complex example, consider the following density plot (requires `ggplot2` to be installed):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[r| require(\"ggplot2\") |]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"[rgraph|\n",
" Xv <- c(rnorm (500, 10,3), rnorm (500, 50, 20), rnorm (500, 70, 20))\n",
" Yv <- c(rnorm (500, 10,3), rnorm (500, 70, 5), rnorm (500, 30, 5))\n",
" myd <- data.frame(Xv, Yv)\n",
"\n",
" ggplot(myd, aes(x = Xv, y = Yv)) + geom_point() + geom_density2d() + theme_bw() |]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit b38c167

Please sign in to comment.