diff --git a/IHaskell/examples/HelloWorld.ipynb b/IHaskell/examples/HelloWorld.ipynb deleted file mode 100644 index 3322e842..00000000 --- a/IHaskell/examples/HelloWorld.ipynb +++ /dev/null @@ -1 +0,0 @@ -{"nbformat_minor": 0, "cells": [{"source": "At first will try to load template haskell. Then we will need our R.", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": ":ext TemplateHaskell QuasiQuotes", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Now we will add H.Prelude", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "initializeEmbeddedR defaultConfig", "outputs": [{"output_type": "display_data", "data": {"text/plain": ""}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "In order to use `H` in your notebook you need to use `H` quasiquote.\nAs you may have noticed `H` itself uses `r` quasiquote, but `r` is\nis already widely used in `ihaskell` by `rlangqq`. And because there\nis no sane way to hide packages, we decided to use `h`:", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "[h| 1 + 1|]", "outputs": [{"output_type": "display_data", "data": {"text/plain": "0x00007fe054a90798"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Huh.. what does it mean? As was written in H manual (link), this an address of the result.\nBut what to do if you want to see value, not somethis strange? \nFor this reason we have introduced the simplest quasi-quoter hDisp that just shows an output in readable form. It's not composable as it always have type IO (), so you can't use it as a part of other expression.", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "[hDisp| 1+1|]", "outputs": [{"output_type": "display_data", "data": {"text/plain": "[1] 2"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Having plots in R is not so straightforward as it seems. In order to output plot\nto the file one need to first set up plot device (one of `pdf`, `bmp`, `png` functions),\nthen once you've added all data use `dev.off()` in order to flush data.\nThis is required because you may want to add additional plots and data to the same one,\nmore over you can do it in a different cells.\nThis mean that you need to write smth like:", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "_ <- [h| png(\"Rplots/5-%03d.png\");plot(c(1:10),mapply(sin,c(1:10))); dev.off()|]", "outputs": [{"output_type": "display_data", "data": {"text/plain": ""}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Do not hope that **R** is smart enough to not rewrite your file, and add some sort of\nsuffix, even if we have \"%03d\" in filename, this is for pages only. As a result everything\nlooks quite scary to use and requite a lots of boilerplate. In order to automate this we\nhave introduced `hplot` quasiquter that hide all details in a background.\n`hplot` do the following:\n 1. initializes *png* device; \n 2. outputs plots to file \"Rplots/auto-%i.png\" where `i` is the highest index to be found there;\n 3. flushes data with `dev.off()`;\n 4. shows plot in the `notebook`.", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "[hPlot| plot(c(1:10), mapply(cos,c(1:10)))|]", "outputs": [{"output_type": "display_data", "data": {"text/plain": "", "text/html": "\n"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Do you think, this is all? More or less so.. however you need to know that Resource handing instance for `IO`,\nis a bit cheaty, it justs protects all toplevel values forever, so not expect from R to return objects anytime\nsoon. For toplevel objects this is expected behaviour, but here we have the same behavior for all values\nappear in quasiquotes, so `[h| sum(allocate10GbVector) |]` will not free 10Gb Vector anytime. And this is not\nwhat you are expecting for research tool. However there is a solution, all you need is to use Regions, read\nmore on the topic in (link).", "cell_type": "markdown", "metadata": {}}, {"execution_count": 46, "cell_type": "code", "source": "import H.Prelude.Interactive\nimport qualified Foreign.R.Type as R", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 47, "cell_type": "code", "source": "runRegion $ fmap (fromSEXP . cast R.SReal) [h| 1 + 1|] :: IO Double", "outputs": [{"output_type": "display_data", "data": {"text/plain": "2.0"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Haskell", "name": "haskell", "language": "haskell"}}} \ No newline at end of file diff --git a/IHaskell/examples/tutorial-ihaskell-inline-r.ipynb b/IHaskell/examples/tutorial-ihaskell-inline-r.ipynb new file mode 100644 index 00000000..ce409ee8 --- /dev/null +++ b/IHaskell/examples/tutorial-ihaskell-inline-r.ipynb @@ -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 +}