In [1]:

```
:ext QuasiQuotes
import qualified H.Prelude as H
H.initialize H.defaultConfig
```

In [2]:

```
-- print output
[rprint| lm(mpg ~ wt, data = mtcars) |]
```

In [3]:

```
-- plot stuff
-- we just require ggplot2 as we do everything directly in R
[rgraph|
require(ggplot2)
ggplot(diamonds, aes(carat, price)) + geom_point() + theme(aspect.ratio = 1) |]
```

Assignment also works as usual:

In [4]:

```
[rprint| fact <- function(n) if(n == 0) 1 else n * fact(n - 1)
fact(10) |]
```

Call fact again...

In [5]:

```
[rprint| fact(10) |]
```

The embedded R interpreter is stateful, but for global assignment we need to use <<-

In [6]:

```
[r| fact <<- function(n) if(n == 0) 1 else n * fact(n - 1) |]
```

In [7]:

```
[rprint| fact(25) |]
```

The _hs suffix is used to splice values from Haskell into the R code:

In [8]:

```
-- a Haskell list
xs = [1..10] :: [Double]
xs
```

In [9]:

```
[rprint| mean(xs_hs) |]
```

We can even use functions defined in Haskell in R:

In [10]:

```
import Data.Int
fact :: Int32 -> Int32
fact 0 = 1
fact n = n * fact (n - 1)
factR x = return $ fact x :: H.R s Int32
```

In [11]:

```
n = 6 :: Int32
[rprint| factR_hs(n_hs) |]
```

In [12]:

```
get_normals :: Double -> H.R s [Double]
get_normals n = do
H.dynSEXP <$> [r| rnorm(n_hs) |]
result <- H.runRegion $ get_normals 4
result
```

Now these are just normal Haskell values you can work with:

In [13]:

```
:t result
```