tweag / HaskellR

The full power of R in Haskell.
https://tweag.github.io/HaskellR
Other
584 stars 47 forks source link

inline-r fails to run on Mac M1 / M2 #407

Open idontgetoutmuch opened 1 year ago

idontgetoutmuch commented 1 year ago

Describe the bug

[nix-shell:~/Packages/HaskellR]$ stack --nix exec -- H
Language.R.Interpreter: Cannot increase stack size limit.Try increasing your stack size limit manually:$ launchctl limit stack 67104768$ ulimit -s 65532
*** Exception: setResourceLimit: permission denied (Operation not permitted)
ghci> [r| print(.libPaths())        |]
Language.R.Interpreter: Cannot increase stack size limit.Try increasing your stack size limit manually:$ launchctl limit stack 67104768$ ulimit -s 65532

To Reproduce Steps to reproduce the behavior.

Expected behavior A clear and concise description of what you expected to happen.

Environment

Additional context Add any other context about the problem here.

idontgetoutmuch commented 1 year ago

Any update on this? I just tried inline-r = super.haskell.lib.dontCheck (hself.callHackage "inline-r" "1.0.0" {}); and got the same error. I tried the manual instructions but with the same error message.

facundominguez commented 1 year ago

Hello! I don't have insights on why it doesn't work on Mac, but if you find some other way to set the stack size of the process, or if such setup is no longer needed, the failing setup should be easy to disable here.

idontgetoutmuch commented 1 year ago
--     setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
--       `onException` (hPutStrLn stderr $
--                        "Language.R.Interpreter: "
--                        ++ "Cannot increase stack size limit."
--                        ++ "Try increasing your stack size limit manually:"
-- #ifdef darwin_HOST_OS
--                        ++ "$ launchctl limit stack 67104768"
--                        ++ "$ ulimit -s 65532"
-- #elif defined(freebsd_HOST_OS)
--                        ++ "$ ulimit -s 67104768"
-- #else
--                        ++ "$ ulimit -s unlimited"
-- #endif
--                     )

seems to work but now plotting does not

Warning message:
In (function (title, width, height, pointsize, family, antialias,  :
  Unable to create Cocoa Quartz window: NSWindow drag regions should only be invalidated on the Main Thread! (NSInternalInconsistencyException)
*** Exception: R Runtime Error: Error in (function (title, width, height, pointsize, family, antialias,  : 
  unable to create quartz() device target, given type may not be supported

But this does work

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
import H.Prelude as H
import Language.R.QQ

main = H.withEmbeddedR defaultConfig $ do
  H.runRegion $ do
    x <- [r| seq(-4, 4, length=100) |]
    y <- [r| dnorm(x_hs) |]
    _ <- [r| jpeg(file="saving_plot1.jpeg") |]
    _ <- [r| plot(x_hs,y_hs, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "") |]
    _ <- [r| dev.off() |]
    return ()

So I can actually do some work now.

Should I create a PR to disable the check for macOS? But what about folks who use pre M1 / M2?

I will create a separate issue for the plotting problem. I tried R on its own and it can plot (I assume via Quartz) so I am not sure why there is a problem via inline-r.

facundominguez commented 1 year ago

The fact that it fails in the interpreter but not when building a standalone executable hints that H may not be invoking R on the main thread.

H.ghci goes through the trouble of setting -fno-ghci-sandbox. Starting ghci manually and passing the flags from H.ghci manually might help debugging.

Should I create a PR to disable the check for macOS?

We could try that if it works in CI. Ideally, we should test whether setResourceLimit is needed in any platform.

idontgetoutmuch commented 1 year ago

The fact that it fails in the interpreter but not when building a standalone executable hints that H may not be invoking R on the main thread.

I might have confused you. I meant to say that I can't get charts drawn interactively but I can write a chart to a file (dev.off()). It's something to do with Quartz. I think I have seen something like it before.

Should I create a PR to disable the check for macOS?

We could try that if it works in CI. Ideally, we should test whether setResourceLimit is needed in any platform.

It won't be for a few days at least as I may not have internet access.