# RCall.jl - how to handle R package dependencies?

**URL:** <https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172>\
**Category:** Package Management\
**Tags:** question, interoperability, rcall\
**Created:** [June 22, 2022, 9:33am UTC](https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172 "2022-06-22T09:33:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat-alt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pat-alt/32/34264_2.png) [@pat-alt](https://discourse.julialang.org/u/pat-alt)\
**Post date:** [June 22, 2022, 9:33am UTC](https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172/1 "2022-06-22T09:33:30Z")

</div>

Hi there,

I’m working on an experimental feature for [CounterfactualExplanations.jl](https://github.com/pat-alt/CounterfactualExplanations.jl) that adds native support for explaining R [`torch`](https://torch.mlverse.org/docs/index.html) models. This is made possible by [RCall.jl](https://juliainterop.github.io/RCall.jl/stable/) and local tests are passing. But since `torch` is not a standard library in R, my CI workflow [fails](https://github.com/pat-alt/CounterfactualExplanations.jl/runs/7000674219?check_suite_focus=true#step:6:331) (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?**

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [July 16, 2024, 3:58pm UTC](https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172/2 "2024-07-16T15:58:37Z")

</div>

```julia
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.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [March 27, 2025, 9:27am UTC](https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172/3 "2025-03-27T09:27:59Z")

</div>

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

> [@\[ANN\] RSetup.jl: mini-R package manager for use with RCall](https://discourse.julialang.org/t/ann-rsetup-jl-mini-r-package-manager-for-use-with-rcall/127396):
>
> using RSetup setup\_r\_environment(["classInt"]) This single-function package will detect if the R environment includes the classInt library (or your choice of libraries) and, if needed, install it so that it is available for use with RCall. It’s on github at [Install RSetup.jl from github](https://github.com/technocrat/RSetup.jl) Not tested on Windows.

> [@pat-alt](#):
>
> **Q1: Is there an elegant/preferred way to install R package dependencies on load?**
> 
> [..]
> 
> **Q2: Is relying on deps of other languages utter madness or feasible?**

@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](https://github.com/Taaitaaiger/jlrs) 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):

> [@Creating app with PackageCompiler.jl --\> how to include R dependency?](https://discourse.julialang.org/t/creating-app-with-packagecompiler-jl-how-to-include-r-dependency/103167/7):
>
> I didn’t think RCall downloaded Julia for you, but I was mistaken, it does if you set R\_HOME = “\*”, using Conda. I’m not sure, that might still be at installation time, but since possible, could at least potentially be a runtime check. About R packages themselves, I think they are usually downloaded as source and compiled, and arguably could also be done at runtime (Julia could have C++\_compiler\_JLL…, maybe does already, to provide all needed infrastructure, it’s just would you want to wait fo…

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [March 27, 2025, 9:47am UTC](https://discourse.julialang.org/t/rcall-jl-how-to-handle-r-package-dependencies/83172/4 "2025-03-27T09:47:28Z")

</div>

I’ve added the [github link for RSetup.jl](https://github.com/technocrat/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.
