R/utils-julia.R

Defines functions jl_require jl_format sanitize_jl_error jl_try_pkg_install jl_pkg_add jl_pkg_installed jl_io

jl_io <- function(verbose) {
  if (verbose) "" else "io=devnull"
}

jl_pkg_installed <- function(x) {
  x %in% loaded_libs()
}

jl_pkg_add <- function(x, ..., verbose = interactive()) {
  if (!jl_pkg_installed(x)) {
    jl('Pkg.add("%2$s"; %1$s); using %2$s', jl_io(verbose), x)
  }
}

jl_try_pkg_install <- function(x, ..., verbose = interactive()) {
  if (!jl_pkg_installed(x)) {
    try(jl_pkg_add(x, verbose = verbose))
  }
  jl_pkg_installed(x)
}

sanitize_jl_error <- function(e, .call) {
  e$message <- gsub("Stacktrace:.*$", "", e$message)
  e$call <- .call
  e
}

jl_format <- function(x, ...) {
  jl_require("JuliaFormatter")
  JuliaConnectoR::juliaCall("JuliaFormatter.format_text", x, align_matrix = TRUE, ...)
}

jl_require <- function(x) {
  installed <- jl_try_pkg_install(x)
  if (!installed) stop("Unable to add package: ", x)
  invisible(installed)
}

Try the jlme package in your browser

Any scripts or data that you put into this service are public.

jlme documentation built on April 3, 2025, 10:29 p.m.