This function connects R to Julia.

julia_connect(
  ...,
  JULIA_PROJ,
  .pkg_config = NULL,
  .pkg_update = FALSE,
  .threads = NULL,
  .verbose = getOption("patter.verbose")
)

Arguments

...

Arguments, such as JULIA_HOME, passed to JuliaCall::julia_setup() (excluding verbose, which is handled below).

JULIA_PROJ

(optional) A character string that defines the directory of a Julia Project.

If missing, the function scans:

  • The global option, JULIA_PROJ;

  • The environmental variable, JULIA_PROJ;

If missing and unfound, JULIA_PROJ = NULL is used with a warning.

If NULL, a Julia Project is not used and the default environment is used (e.g., ~/.julia/environments/v1.10/Project.toml).

.pkg_config

(optional) A character string of Julia code, evaluated by julia_code(), that configures Julia prior to dependency management.

.pkg_update

A logical variable that defines whether or not to update installed Julia packages.

.threads

A character ("auto") or an integer that defines the number of threads used by multi-threaded operations in Julia. This can only be set once per R session. It is currently ignored on Windows (see below).

.verbose

User output control (see patter-progress for supported options).

Value

The function returns the Julia interface invisibly (see JuliaCall::julia_setup()).

Details

patter is an R front-end for the Patter.jl package. This requires a local installation of Julia. This function connects R to the local Julia installation, sets up JuliaCall, which provides the integration between R and Julia, and Patter.jl. Internally, the steps are as follows:

  • JuliaCall is set up via JuliaCall::julia_setup().

  • The number of threads is set via the JULIA_NUM_THREADS environment variable:

    • If .threads = NULL, JULIA_NUM_THREADS is set to "auto" if unset or left unchanged otherwise;

    • Otherwise, JULIA_NUM_THREADS is set to .threads;

    • The number of threads can only be set once per R session;

    • On Windows, setting JULIA_NUM_THREADS as an environment variable in .Renviron or using .threads does not work. See this issue for a workaround.

  • The Julia installation is validated.

  • A local Julia Project is generated in JULIA_PROJ (if specified and required) and activated. We recommend using patter within an RStudio Project, with a Julia directory at the top-level that contains the Julia project.

  • If specified, .pkg_config is run via julia_code().

  • Patter.jl and supporting dependencies are installed or updated (if required) and loaded (optionally in the local Julia Project). If the environment variable PATTER.JL_DEV = "path/to/local/clone/of/Patter.jl" is set, Patter.jl is installed from a local source as a development dependency (via Pkg.develop()); otherwise, Patter.jl is installed from the remote.

You should run this function once per R session.

To update the number of threads, restart R and re-run the function with an updated .threads argument.

See also

See julia_validate() to validate the R---Julia interface.

Author

Edward Lavender

Examples

if (julia_run()) {

  #### Example (1): First time use
  # Use `...` to customise `JuliaCall::julia_setup()`
  # Try `installJulia` if you require a Julia installation
  julia_connect(installJulia = TRUE)

  #### Example (2): Connect to `Julia` using default settings
  # You may need to tell `R` where Julia is via `JULIA_HOME`
  julia_connect()

  #### Example (3): Use a local `Julia` Project (recommended)
  proj <- file.path(tempdir(), "Julia")
  julia_connect(JULIA_PROJ = proj)

  #### Example (4): Force an update of installed packages
  if (FALSE) {
    julia_connect(JULIA_PROJ = proj, .pkg_update = TRUE)
  }

  #### Example (5): Specify the number of threads
  # You can only set threads once per `R` session!
  julia_connect(JULIA_PROJ = proj, .threads = 2L)

  #### Example (6): Customise user output
  julia_connect(JULIA_PROJ = proj, .verbose = FALSE)

  file_cleanup(proj)
}
#> `patter::julia_connect()` called @ 2024-08-01 11:40:31... 
#> ... Running `Julia` setup via `JuliaCall::julia_setup()`... 
#> ... Validating Julia installation... 
#> ... Setting up Julia project... 
#> ... Handling dependencies... 
#> ... `Julia` set up with 8 thread(s). 
#> `patter::julia_connect()` call ended @ 2024-08-01 11:40:36 (duration: ~5 sec(s)). 
#> `patter::julia_connect()` called @ 2024-08-01 11:40:36... 
#> ... Running `Julia` setup via `JuliaCall::julia_setup()`... 
#> ... Validating Julia installation... 
#> ... Setting up Julia project... 
#> ... Handling dependencies... 
#> ... `Julia` set up with 8 thread(s). 
#> `patter::julia_connect()` call ended @ 2024-08-01 11:40:41 (duration: ~5 sec(s)). 
#> `patter::julia_connect()` called @ 2024-08-01 11:40:41... 
#> ... Running `Julia` setup via `JuliaCall::julia_setup()`... 
#> ... Validating Julia installation... 
#> ... Setting up Julia project... 
#> ... Handling dependencies... 
#> ... `Julia` set up with 8 thread(s). 
#> `patter::julia_connect()` call ended @ 2024-08-01 11:41:02 (duration: ~21 sec(s)). 
#> `patter::julia_connect()` called @ 2024-08-01 11:41:02... 
#> ... Running `Julia` setup via `JuliaCall::julia_setup()`... 
#> Warning: Restart `R` to update the number of threads in `Julia`.
#> ... Validating Julia installation... 
#> ... Setting up Julia project... 
#> ... Handling dependencies... 
#> Warning: `JULIA_NUM_THREADS` could not be set via `.threads`.
#> ... `Julia` set up with 8 thread(s). 
#> `patter::julia_connect()` call ended @ 2024-08-01 11:41:08 (duration: ~6 sec(s)). 
#> Warning: `JULIA_NUM_THREADS` could not be set via `.threads`.