-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting values from R into the 'pure' Haskell world (using inline-r in source files) #330
Comments
Hello! Sorry for the late response.
however that road may lead to failures. I'll try to describe potential problems tomorrow, so maybe we will resolve if your problem can be restructured so IO nature of R calls will not be a problem, or if we can provide safe API that would allow you to keep non-IO API. |
Thanks for the reply! My present use case is that I've written a small Markov Chain module in Haskell which takes in a dataset and pre-processes it into set of data structures suitable for interactive use in the rest of the program. My code to load and transform the tabular dataset into the required shape for the Markov Chain module to ingest is written in R, which would be then passed into Haskell for processing by the Markov Chain module and the resulting data structures sent back to R for further analysis/plotting/logging (at this point the program would be in IO). As my Markov Chain module is all pure code, I'd like to be able to get the R data into Haskell 'cleanly', so that it can be pre-processed purely before entering IO for the interactive aspects of the system. I feel that the example you've provided for I also read in #294 that there's a new Thanks again! |
There are several problems, the main one is that in order for you to be safe you need to guarantee that all data lives in protection stack. So this means that you will use a single Region (due to Parser was renamed to matcher, you can find documentation on: https://hackage.haskell.org/package/inline-r-0.9.1/docs/Language-R-Matcher.html In may have missing helpers but was enough for our usecase, so feel free to request more helper functions there if needed.
Lives in |
Thanks very much again. I'll be exploring this in depth over the next few days. |
Had a chance to explore these suggestions today. Have been working on other (pure) modules for a while. Using the regular [r| ... |] quasi-quotation, I can represent a (toy example) call to R like this:
Which I can then call in Haskell IO code as such:
Which (correctly!) returns: This is a workflow that I could work with for now. However, what I actually need to do is to bring a list of (varying length) atomic vectors into Haskell (it's actually fine for me to work inside IO in the above manner). I tried out this kind of code:
But this won't compile. If I change the type signature to
This seems like an obvious error, but I'm not sure how I should represent a list of vectors to get it into Haskell. Ideally I'd like to represent it as a list of lists although for now any insight is valuable. Without a type signature, it will also compile but returns a different error:
I also tried to work with the [rsafe| ... |] quasi-quotation, but I didn't have any success with that. I simplified the example given above into this:
Which compiles and runs fine (although returns a pointer value:
Then it just fails to compile. I didn't manage to get any further in this case. Thanks again for the help and insight so far. It's been really valuable and I really appreciate the high quality of knowledge that you've shared. I love the scope of the library and would like to become more competent with it! |
I also had a try and couldn't get to grips with the In fact, are there any resources in general featuring example code using the As I manage to get more competent at performing practical tasks later, I'll be happy to help create code examples! Thanks again and best regards, |
Some examples can be found on @idontgetoutmuch |
Thanks very much for those links. While I'd browsed that blog previously, in checking the specific links you sent just now I noticed that the method of extracting dataframe columns used there fits into the code I was working with very simply:
Since R dataframe columns are lists, this is bringing a list of 'length 1' atomic vectors in from R. The specific column of data that I'd like to bring in looks like this: (a list of atomic vectors of varying length) Is it possible for me to get this data into Haskell from R? I'd actually prefer to do this with the [rsafe| ... |] quasi-quotation code for now, but I'd value any insight into the preferable way to do this. Thanks again for all the help, |
You can definitely do that with matcher, I'll try to comeup with code today. And also I'll check if that can be done by the simpler means. I think you can try to use |
Thanks. It'll give me a starting point to explore matcher more deeply from too. |
I've been working on other modules for a while so I've not really looked much at this. Today I came back to it and read through the Matcher documentation on Hackage. The module seems quite simple and logical and seems to do exactly what I want to do with it's I can't find anything about it anywhere else on the internet. If you get a chance, if you could provide any example of a use case that I could start from then it'd me much appreciated. Thanks, |
I actually managed to do what I needed to do by reducing the list column with I'd still be interested to learn how to use Matcher though (and while this works, it'd definitely be a better solution) so any reference material would be appreciated. |
I've managed to get data into Haskell using the insight gained from reading this issue:
#294
This prints to terminal fine in the REPL, however, I'm using
inline-r
in compiled source code, rather than using H or iHaskell. The problem I'm running into is that my incoming data is getting 'stuck in IO', where in reality I'd prefer to get that data into Haskell's pure universe, for further processing.The test functions I'm using (with types) are the following to define what data I want to retrieve:
(I'd actually prefer to use fromSEXP with a type rather than fromSomeSEXP, but I couldn't get that working yet)
Then the following brings that data into Haskell.
(these are pretty much taken from the previously mentioned Issue, I just delivered the argument differently and replaced dynSEXP with fromSomeSEXP.
On a quick tangent, I also tried to generalise the return function:
This failed, however, so I discarded this approach for the time being.
I do understand that I can most likely (certainly!) get this to work in a more optimised way (and I do understand the inner workings of R to a workable level). Right now though I'm still digesting the
inline-r
documentation available while building up my Haskell fluency (which is 'ok' at this point), so I'm currently prototyping the program I'm working on using relatively small data and approaching problems in the most understandable way (for me at least, at this time).Could anyone who knows the library more intimately than me share any insight into preferable approaches to the problem I'm exploring?
Thanks,
Oscar
ps.
As an additional 'add on' -- how would I get
Int
data into Haskell from R using the above functions? I've not had any problem withString
orDouble
, butInt
hasn't worked how I'd have expected it to.The text was updated successfully, but these errors were encountered: