Getting started with tutorial-ihaskell-inline-r, "R Runtime Error"

39 views
Skip to first unread message

Stephen J. Barr

unread,
Jan 12, 2016, 3:55:36 PM1/12/16
to haskellr
Greetings,

I am trying to get started. I built and ran the docker container as per the instructions and ran with 

> stack --docker exec ipython notebook

Stepping through the notebook, I can define a factorial function using R code and it seems to evaluate fine. However, when trying to use the function, I get:


R Runtime Error: Error in (function () : could not find function "fact"
The same thing happens when trying to use ggplot2. It seems to require fine, but cannot find the function. Attached is a screenshot.
Thanks for the pointers.-Stephen
Screenshot_2016-01-12_12-54-45.png

Stephen J. Barr

unread,
Jan 12, 2016, 4:00:08 PM1/12/16
to haskellr
As a simple example, it seems like there is an issue with variable binding between different cells of the ipyhthon notebook. See the code below for details. 

Boespflug, Mathieu

unread,
Jan 13, 2016, 5:30:49 AM1/13/16
to Stephen J. Barr, haskellr
Hi Stephen,

the release version of inline-r on Hackage should work fine.

It seems that you're running into issues on the `master` branch, i.e. the bleeding edge of development.  We're in the process of changing slightly how quasiquotes behave. While in the past all quasiquotes would run in the global environment, we now have them run in their own local child environment of the parent environment. As a result, where you could previously do

ghci> [r| f <- function(x) x + 1 |]
ghci> [r| f(1) |]

using the current inline-r master branch you should now do

ghci> [r| f <<- function(x) x + 1 |]
ghci> [r| f(1) |]

That is, use the <<- operator rather than the <- operator.

The reasons for this change are:

* it makes the semantics for quasiquotations easier to understand: [r| c(x_hs, y_hs, z_hs) |] is syntactic sugar for applying the following R function:

function(x, y, z) c(x, y, z)

* to our knowlegde there is no reliable way in R to unset bindings in the global environment. Meaning that we could have added a binding to the global environment for each antiquotation variable instead but then it becomes hard to remove these bindings after executing the code of each antiquotaton.
* The <- operator shadows bindings from the base library without warning. This means that the referential transparency of pure quasiquotes can be compromised: you could add a new binding for "+" and then any quasiquote that happens to run after you do that rather than before would now return a different result.
* The <<- operator on the other hand refuses to rebind any "locked" binding (i.e. pretty much all bindings that are part of same package, be it on CRAN or in the standard library). So you can't accidentally change the meaning of an R expression.

This is a proposed change currently in master, which may or may not survive by release time. At any rate I'll update the ipython tutorial shortly to reflect the change.

Best,

--
Mathieu Boespflug
Founder at http://tweag.io.
Reply all
Reply to author
Forward
0 new messages