Using R and JuliaCall is not reliable without assigning R environment variables before starting R

Suppose the file “sourceCode.R” contains an R script that calls a Julia function using julia_call(), which is defined in the R package JuliaCall.

If R is started in a Linux terminal using the command “R”, then the R command “source(sourceCode.R)” produces a segmentation fault (core dumped).

In contrast, if R is started in a Linux terminal using the following code:

R_HOME=/usr/lib/R
export R_HOME
R_SHARE_DIR=/usr/share/R/share
export R_SHARE_DIR
R_INCLUDE_DIR=/usr/share/R/include
export R_INCLUDE_DIR
R_DOC_DIR=/usr/share/R/doc
export R_DOC_DIR
exec /usr/lib/R/bin/exec/R

then the R command “source(sourceCode.R)” executes without errors.

Evidently, explicitly setting these R-related environment variables to their correct system paths BEFORE running R is needed to ensured that both R and any external libraries it calls (like JuliaCall) have the precise, necessary information to locate all R components.

This correct path information allows the underlying inter-language connection to be established cleanly, preventing the memory access error.

Unfortunately, running R from the Linux terminal is not nearly as convenient as running R within Emacs (or another editor), where R code can be edited and run more easily. Does anyone have an alternative solution to this problem?

FYI: I am using Julia 1.12.1 and Ubuntu 24.04.

1 Like

Am I missing something? Why not put those in your .bashrc/.zshrc?

I tried executing all lines except “exec /usr/lib/R/bin/exec/R” and then executing “R”. Doing so results in a segmentation fault.

I get a segment fault, also, on substantially the same setup. This, however, works

# miscellaneous.jl
function agm(a, b; tol = 1.0e-15)
    a0 = a; b0 = b
    while abs(b0-a0) >= tol
        a0, b0 = (a0 + b0)/2.0, sqrt(a0 * b0)
    end
    return (a0 + b0) / 2.0
end
library(JuliaCall)
julia_source("miscellaneous.jl")
julia_exists("agm")
TRUE

My original post should have stated:
If R is started in a Linux terminal using the command “R”, then the R command “source(sourceCode.R)” may produce a segmentation fault (core dumped).

That is, a segmentation fault is not produced for every R source code. But occurrence of a segmentation fault is certainly not uncommon.

This line is a bit uncommon for me. Why not just

/usr/lib/R/bin/exec/R

why the exec?
Bash man-page tells:

       exec [-cl] [-a name] [command [arguments]]
              If  command  is specified, it replaces the shell.  No new process is created.  The arguments become the arguments to command.  If the -l option is supplied, the shell
              places a dash at the beginning of the zeroth argument passed to command.  This is what login(1) does.  The -c option causes command to be executed with an empty envi-
              ronment.   If  -a is supplied, the shell passes name as the zeroth argument to the executed command.  If command cannot be executed for some reason, a non-interactive
              shell exits, unless the execfail shell option is enabled.  In that case, it returns failure.  An interactive shell returns failure if the file cannot be executed.   A
              subshell  exits  unconditionally if exec fails.  If command is not specified, any redirections take effect in the current shell, and the return status is 0.  If there
              is a redirection error, the return status is 1.

Replacing the shell is probably something special, which may have some use cases. I never had to use it, that’s why I stumbled over it. So, perhaps, there is not a problem at all when you just run R with

/usr/lib/R/bin/exec/R

I didn’t do any experiments, just a guess into nothing. You may try if you like.

This solution was suggested at https://github.com/JuliaInterop/JuliaCall/issues/238

The idea is to assign the R environment variables and to launch R using one shell script.

However, this is no longer needed because the problems with the JuliaCall package have been resolved in a VERY RECENT update of the package, which can be downloaded from https://github.com/JuliaInterop/JuliaCall

Robert

2 Likes