|
| 1 | +# Globals ----------------------------------------------------------------- |
| 2 | +acro_venv <- "r-acro" |
| 3 | +acro_pkg <- "acro==0.4.11" |
| 4 | +ch <- "conda-forge" |
| 5 | + |
| 6 | + |
| 7 | +#' Resolve Python executable |
| 8 | +#' |
| 9 | +#' @return String path to a Python executable. |
| 10 | +## nocov start |
| 11 | +get_python <- function() { |
| 12 | + python <- Sys.which("python3") |
| 13 | + if (python == "") { |
| 14 | + python <- Sys.which("python") |
| 15 | + if (python == "") { |
| 16 | + stop("Python not found in PATH. Please ensure Python is installed.") |
| 17 | + } |
| 18 | + } |
| 19 | + return(python) |
| 20 | +} |
| 21 | +## nocov end |
| 22 | + |
| 23 | + |
| 24 | +#' Install ACRO in a conda environment |
| 25 | +#' |
| 26 | +#' @param envname Name of the conda environment |
| 27 | +## nocov start |
| 28 | +install_conda <- function(envname) { # nocov |
| 29 | + if (!reticulate::condaenv_exists(envname = envname, conda = "auto")) { |
| 30 | + reticulate::conda_create(envname = envname, python_version = "3.12", channel = ch) |
| 31 | + reticulate::conda_install(envname = envname, packages = acro_pkg, channel = ch) |
| 32 | + } |
| 33 | +} |
| 34 | +## nocov end |
| 35 | + |
| 36 | + |
| 37 | +#' Initialise ACRO in a Python virtual environment |
| 38 | +#' |
| 39 | +#' @param envname Name of the virtual environment |
| 40 | +install_venv <- function(envname = acro_venv) { |
| 41 | + if (!reticulate::virtualenv_exists(envname)) { |
| 42 | + python <- get_python() |
| 43 | + |
| 44 | + reticulate::virtualenv_create( |
| 45 | + envname = envname, |
| 46 | + python = python, |
| 47 | + force = TRUE, |
| 48 | + packages = NULL |
| 49 | + ) |
| 50 | + |
| 51 | + reticulate::py_install(acro_pkg, envname = envname) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
1 | 56 | #' Initialise an ACRO object |
2 | 57 | #' |
3 | 58 | #' @param suppress Whether to automatically apply suppression. |
| 59 | +#' @param envname Name of the Python environment to use. |
| 60 | +#' @param use_conda Whether to use a Conda environment (`TRUE`) or venv (`FALSE`). |
4 | 61 | #' |
5 | | -#' @return No return value, called for side effects |
| 62 | +#' @return Invisibly returns the ACRO object, which is used internally. |
6 | 63 | #' @export |
| 64 | +acro_init <- function(suppress = FALSE, envname = acro_venv, use_conda = FALSE) { |
| 65 | + # initialise the environment |
| 66 | + if (!reticulate::py_available(initialize = FALSE)) { |
| 67 | + if (use_conda) { # nocov |
| 68 | + install_conda(envname) # nocov |
| 69 | + reticulate::use_condaenv(envname, required = TRUE) # nocov |
| 70 | + } else { |
| 71 | + install_venv(envname) |
| 72 | + reticulate::use_virtualenv(envname, required = TRUE) |
| 73 | + } |
| 74 | + } |
7 | 75 |
|
8 | | -acro_init <- function(suppress = FALSE) { |
9 | | - create_virtualenv() |
| 76 | + # import the acro package and instantiate an object |
10 | 77 | acro <- reticulate::import("acro", delay_load = TRUE) |
11 | 78 | acroEnv$ac <- acro$ACRO(suppress = suppress) |
| 79 | + |
| 80 | + invisible(acroEnv$ac) |
12 | 81 | } |
0 commit comments