How to get the R value in GHCI ?

49 views
Skip to first unread message

Stéphane Laurent

unread,
Aug 31, 2016, 12:27:15 PM8/31/16
to haskellr
Hello,
 For example, how can we get 2 from [r|1+1] ? 
I've seen some documentations about evaluating R expressions in H, but how to do in ordinary GHCI ?

Boespflug, Mathieu

unread,
Aug 31, 2016, 12:49:04 PM8/31/16
to Stéphane Laurent, haskellr
H is ordinary GHCi. It's just GHCi initialized in such a way that a handful of modules are in scope and a few language extensions are enabled by default. See here for the initialization script we use:


So getting a Haskell value from a quasiquote works exactly the same way as in H:

> p [r| 1 + 1 |]
[1] 2

The p function above is a shorthand for calling R's printing function to display the value of the quasiquote on the terminal.

If you want an actual Haskell value, you need a conversion:

> x <- [r| 1 + 1 |]
> fromSomeSEXP x :: Double
2.0

Note that what you'll get back is a floating point number, not an integer. That's because the "1" literal in R actual stands for a a floating point number. You can tell R you really want a (32-bit) integer though:

> import Data.Int
> y <- [r| 1L + 1L |]
> fromSomeSEXP x :: Int32
2

HTH,

--
Mathieu Boespflug
Founder at http://tweag.io.

--
You received this message because you are subscribed to the Google Groups "haskellr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskellr+unsubscribe@googlegroups.com.
To post to this group, send email to hask...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/haskellr/bfed050d-85dd-40be-8032-7f5d65f090fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stéphane Laurent

unread,
Aug 31, 2016, 1:00:34 PM8/31/16
to haskellr
Thank you !

Stéphane Laurent

unread,
Aug 31, 2016, 3:06:23 PM8/31/16
to haskellr
Now to use it in a module, I'm doing :
{-# OPTIONS_GHC -fno-ghci-sandbox #-}
{-# LANGUAGE QuasiQuotes       #-}

{-# LANGUAGE TemplateHaskell   #-}

{-# LANGUAGE ViewPatterns      #-}

{-# LANGUAGE DataKinds #-}

{-# LANGUAGE GADTs #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE ScopedTypeVariables #-}
import H.Prelude.Interactive as H.Prelude

But I don't know how to deal with 
Language.R.Instance.initialize Language.R.Instance.defaultConfig
?

Boespflug, Mathieu

unread,
Aug 31, 2016, 3:10:00 PM8/31/16
to Stéphane Laurent, haskellr
Call that function early on from your main function. Better yet, use Language.R.Instance.withEmbeddedR.


--
Mathieu Boespflug
Founder at http://tweag.io.

--
You received this message because you are subscribed to the Google Groups "haskellr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskellr+unsubscribe@googlegroups.com.
To post to this group, send email to hask...@googlegroups.com.

Stéphane Laurent

unread,
Aug 31, 2016, 4:58:57 PM8/31/16
to haskellr
This code works: 

:set -XQuasiQuotes
import qualified Foreign.R as R
import Foreign.R (SEXP, SEXPTYPE)
import Language.R.Instance as R
import Language.R.QQ
import H.Prelude (fromSomeSEXP)

z
<-  R.withEmbeddedR R.defaultConfig $  R.unsafeRunRegion [r|1+1|]
fromSomeSEXP z
:: Double

But if I do "z <- ..." a second time,  GHCI crashes after returning the message "R already initialized". 

Stéphane Laurent

unread,
Aug 31, 2016, 5:33:22 PM8/31/16
to haskellr
Call that function early on from your main function.

Ok, I've managed by using this way.

Stéphane Laurent

unread,
Sep 1, 2016, 5:37:11 AM9/1/16
to haskellr
My goal was to try to do a web app which calls R, and it works: http://stla.github.io/stlapblog/posts/RunRInYesod.html
Very cool.

Boespflug, Mathieu

unread,
Sep 1, 2016, 7:46:44 AM9/1/16
to Stéphane Laurent, haskellr
Very cool indeed!

--
Mathieu Boespflug
Founder at http://tweag.io.

On 1 September 2016 at 11:37, 'Stéphane Laurent' via haskellr <hask...@googlegroups.com> wrote:
My goal was to try to do a web app which calls R, and it works: http://stla.github.io/stlapblog/posts/RunRInYesod.html
Very cool.

--
You received this message because you are subscribed to the Google Groups "haskellr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskellr+unsubscribe@googlegroups.com.
To post to this group, send email to hask...@googlegroups.com.
Message has been deleted

Stéphane Laurent

unread,
Sep 1, 2016, 12:23:49 PM9/1/16
to haskellr
fromSomeSEXP x :: Double

How to do when the result is an integer ? I can't get it:

> fromSomeSEXP y :: Int
<interactive>:35:1:
    
No instance for (Literal Int form0)
      arising 
from a use of fromSomeSEXP
    
In the expression: fromSomeSEXP y :: Int
    
In an equation for it’: it = fromSomeSEXP y :: Int

> fromSomeSEXP y :: Int
<interactive>:35:1:
    
No instance for (Literal Int form0)
      arising 
from a use of fromSomeSEXP
    
In the expression: fromSomeSEXP y :: Int
    
In an equation for it’: it = fromSomeSEXP y :: Int
 

Stéphane Laurent

unread,
Sep 1, 2016, 12:27:38 PM9/1/16
to haskellr
I get it !

> import Data.Int (Int32)
> fromSomeSEXP y :: Int32
2



Boespflug, Mathieu

unread,
Sep 1, 2016, 12:27:58 PM9/1/16
to Stéphane Laurent, haskellr
See my earlier email - you'll want to cast to Data.Int.Int32, not Int. We use explicit explicitly sized integer types to avoid overflow problems as you pass values from one language to another.

> import Data.Int
> fromSomeSEXP x :: Int32

--
Mathieu Boespflug
Founder at http://tweag.io.

On 1 September 2016 at 18:21, 'Stéphane Laurent' via haskellr <hask...@googlegroups.com> wrote:
fromSomeSEXP x :: Double

How to do when the result is an integer ? I can't get it:

> fromSomeSEXP x :: Int

<interactive>:35:1:
   
No instance for (Literal Int form0)
      arising
from a use of fromSomeSEXP
   
In the expression: fromSomeSEXP y :: Int
   
In an equation for it’: it = fromSomeSEXP y :: Int

> fromSomeSEXP x :: Int

<interactive>:35:1:
   
No instance for (Literal Int form0)
      arising
from a use of fromSomeSEXP
   
In the expression: fromSomeSEXP y :: Int
   
In an equation for it’: it = fromSomeSEXP y :: Int
 

--
You received this message because you are subscribed to the Google Groups "haskellr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskellr+unsubscribe@googlegroups.com.
To post to this group, send email to hask...@googlegroups.com.

Stéphane Laurent

unread,
Sep 2, 2016, 8:32:12 AM9/2/16
to haskellr
Another blog post, thanks to your help.
Reply all
Reply to author
Forward
0 new messages