RCall.jl - how to handle R package dependencies?

Hi there,

I’m working on an experimental feature for CounterfactualExplanations.jl that adds native support for explaining R torch models. This is made possible by RCall.jl and local tests are passing. But since torch is not a standard library in R, my CI workflow fails (as it should). So I’m wondering:

Q1: Is there an elegant/preferred way to install R package dependencies on load?

I’m conscious that by having my Julia package depend on R package I might be opening up a can of worms, but having native support for this kind of interoperability would be awesome. I’m planning to restrict myself to torch and perhaps one or two other packages in the long run. Still, I’m wondering:

Q2: Is relying on deps of other languages utter madness or feasible?

function __init__()
   only(rcopy(R"""require("mypkg")""")) && return nothing
   @info "mypkg not available in R, installing now...."
   reval("""install.packages("mypkg", method="wget", repos="https://cloud.r-project.org")""")
   reval("""library("mypkg")""")
   return nothing
end
"""

require returns a boolean about whether a package was successfully loaded, while library errors if the package can’t be loaded.

3 Likes

There is now RSetup.jl, but apparently not yet registered (and I’m not sure where it is!):

@technocrat, before this single function package is registered, maybe its function should just be added to RCall.jl? On Q2, in general I think it’s not madness (calling C code, or even better e.g. Rust, see GitHub - Taaitaaiger/jlrs: Julia bindings for Rust or calling to or from Python using PythonCall.jl), but for R, it’s not feasible if you want to distribute your programs without copyleft/GPL (that’s ok for many science/non-commercial stats users though):

1 Like

I’ve added the github link for RSetup.jl

I don’t have any immediate plans to register because I’d like to see it have a decent opportunity to be shot full of arrows before and, as you suggest, it might better work as part of RCall.