{"action":"created","issue":{"url":"https://api.github.com/repos/tweag/HaskellR/issues/330","repository_url":"https://api.github.com/repos/tweag/HaskellR","labels_url":"https://api.github.com/repos/tweag/HaskellR/issues/330/labels{/name}","comments_url":"https://api.github.com/repos/tweag/HaskellR/issues/330/comments","events_url":"https://api.github.com/repos/tweag/HaskellR/issues/330/events","html_url":"https://github.com/tweag/HaskellR/issues/330","id":338527500,"node_id":"MDU6SXNzdWUzMzg1Mjc1MDA=","number":330,"title":"Getting values from R into the 'pure' Haskell world (using inline-r in source files)","user":{"login":"OscarSouth","id":32574713,"node_id":"MDQ6VXNlcjMyNTc0NzEz","avatar_url":"https://avatars1.githubusercontent.com/u/32574713?v=4","gravatar_id":"","url":"https://api.github.com/users/OscarSouth","html_url":"https://github.com/OscarSouth","followers_url":"https://api.github.com/users/OscarSouth/followers","following_url":"https://api.github.com/users/OscarSouth/following{/other_user}","gists_url":"https://api.github.com/users/OscarSouth/gists{/gist_id}","starred_url":"https://api.github.com/users/OscarSouth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OscarSouth/subscriptions","organizations_url":"https://api.github.com/users/OscarSouth/orgs","repos_url":"https://api.github.com/users/OscarSouth/repos","events_url":"https://api.github.com/users/OscarSouth/events{/privacy}","received_events_url":"https://api.github.com/users/OscarSouth/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-05T11:02:28Z","updated_at":"2018-07-08T21:16:36Z","closed_at":null,"author_association":"NONE","body":"I've managed to get data into Haskell using the insight gained from reading this issue:\r\nhttps://github.com/tweag/HaskellR/issues/294\r\n\r\nThis 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.\r\n\r\nThe test functions I'm using (with types) are the following to define what data I want to retrieve:\r\n\r\n```\r\nrDef  :: Double -> R s [Double]\r\nrDef n = do\r\n  R.fromSomeSEXP <$> [r| rnorm(n_hs) |]\r\n```\r\n(I'd actually prefer to use fromSEXP with a type rather than fromSomeSEXP, but I couldn't get that working yet)\r\n\r\nThen the following brings that data into Haskell.\r\n```\r\nrRet  :: Double -> IO [Double]\r\nrRet n = runRegion $ rDef n\r\n```\r\n(these are pretty much taken from the previously mentioned Issue, I just delivered the argument differently and replaced dynSEXP with fromSomeSEXP.\r\n\r\nOn a quick tangent, I also tried to generalise the return function:\r\n```\r\nrRet'  :: (Double -> R s [Double]) -> Double -> IO [Double]\r\nrRet' f n = runRegion $ f n\r\n```\r\nThis failed, however, so I discarded this approach for the time being.\r\n\r\nI 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).\r\n\r\nCould anyone who knows the library more intimately than me share any insight into preferable approaches to the problem I'm exploring?\r\n\r\nThanks,\r\nOscar\r\n\r\nps.\r\nAs an additional 'add on' -- how would I get `Int` data into Haskell from R using the above functions? I've not had any problem with `String` or `Double`, but `Int` hasn't worked how I'd have expected it to."},"comment":{"url":"https://api.github.com/repos/tweag/HaskellR/issues/comments/403317858","html_url":"https://github.com/tweag/HaskellR/issues/330#issuecomment-403317858","issue_url":"https://api.github.com/repos/tweag/HaskellR/issues/330","id":403317858,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMzMxNzg1OA==","user":{"login":"qnikst","id":170227,"node_id":"MDQ6VXNlcjE3MDIyNw==","avatar_url":"https://avatars2.githubusercontent.com/u/170227?v=4","gravatar_id":"","url":"https://api.github.com/users/qnikst","html_url":"https://github.com/qnikst","followers_url":"https://api.github.com/users/qnikst/followers","following_url":"https://api.github.com/users/qnikst/following{/other_user}","gists_url":"https://api.github.com/users/qnikst/gists{/gist_id}","starred_url":"https://api.github.com/users/qnikst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qnikst/subscriptions","organizations_url":"https://api.github.com/users/qnikst/orgs","repos_url":"https://api.github.com/users/qnikst/repos","events_url":"https://api.github.com/users/qnikst/events{/privacy}","received_events_url":"https://api.github.com/users/qnikst/received_events","type":"User","site_admin":false},"created_at":"2018-07-08T21:16:36Z","updated_at":"2018-07-08T21:16:36Z","author_association":"MEMBER","body":"Hello! Sorry for the late response.\r\nTechnically all calls to R involve foreign library calls, mutable memory operations and handling of the objects lifetime. So they must be in IO. However there is an `rsafe` quasi-quote that allow you to splice your R operation into the pure code, so you can write:\r\n```\r\nfoo = runRegion $ do\r\n   let x = [rsafe| calltoR|]\r\n         y = anotherPureFunction x\r\n   return y\r\n```\r\nhowever 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."}}
{"actor":{"display_login":"qnikst"}}