--- title: "Simple regression with Yesod and R" date: "2016-09-04" --- ```{r setup, include=FALSE} knitr::knit_engines$set(ghc = function (options) { engine = options$engine f = basename(tempfile(engine, ".", ".txt")) writeLines(options$code, f) on.exit(unlink(f)) code = paste(f, options$engine.opts) cmd = options$engine.path out = if (options$eval) { message("running: ", cmd, " ", code) tryCatch(system2(cmd, code, stdout = TRUE, stderr = FALSE, env = options$engine.env), error = function(e) { if (!options$error) stop(e) paste("Error in running command", cmd) }) } else "" if (!options$error && !is.null(attr(out, "status"))) stop(paste(out, collapse = "\n")) knitr::engine_output(options, options$code, out) } ) ## chunk options knitr::opts_chunk$set(engine = 'ghc', engine.path = 'ghcscriptrender', engine.opts = '--module --fragment', echo = FALSE, results = 'asis', cache = TRUE) ``` As announced in [a previous article](http://stla.github.io/stlapblog/posts/ocpusimplereg.html), I did an app with `opencpu` that performs a simple linear regression in R and returns a report. Now I have converted this app to a [Yesod+R app](http://stla.github.io/stlapblog/posts/RunRInYesod.html). Thus it does not use `opencpu` anymore. Only Haskell and standard JavaScript libraries. The contents of this article are my notes about the way I achieved this goal. ## Structure of the folder This folder is available in [a Github repo](https://github.com/stla/yesodsimplereg). ```bash . ├── .cabal-sandbox -> /home/stla/.cabal-sandbox/ ├── cabal.sandbox.config -> /home/stla/cabal.sandbox.config ├── FileToBase64.hs ├── index.hamlet ├── R │   ├── child_regression.Rmd │   ├── knitRegression.R │   └── regression.Rmd ├── simplereg.hs └── static ├── bootstrap │   ├── bootstrap-4.0.0.min.css │   ├── bootstrap-4.0.0.min.js │   └── bootstrap.file-input.js ├── css │   └── regression.css ├── jqplot-1.0.9 │   ├── jquery.jqplot.min.css │   ├── jquery.jqplot.min.js │   └── plugins │   ├── jqplot.canvasAxisLabelRenderer.js │   ├── jqplot.canvasTextRenderer.js │   ├── jqplot.cursor.js │   ├── jqplot.highlighter.js │   └── jqplot.trendline.js ├── jquery │   └── jquery-1.10.2.min.js ├── js │   ├── jsontotable.js │   └── main.js ├── jsonTable │   └── jsonTable.js └── PapaParse └── papaparse-4.1.2.min.js ``` - `simplereg.hs` is the main Haskell code; - `FileToBase64` is an auxiliary module called by `simplereg.hs`; - `index.hamlet` is the html code in hamlet format; - the `static` folder contains all `js` and `css` files; - the `R` folder contains an `R` script and auxiliary files. ## The hamlet file, the main `css`, and the main `js` - Take the file [index.html](https://github.com/stla/ocpusimplereg/blob/master/inst/www/index.html). - Remove all the `css`, put it in the main `css` file `static/css/regression.css` (or another `css` file). - Remove all the remote `