Skip to content

Commit 94a7298

Browse files
committed
feat: add support for conda environments
1 parent ddb5da0 commit 94a7298

10 files changed

Lines changed: 117 additions & 117 deletions

R/acro_init.R

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,77 @@
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+
get_python <- function() {
11+
python <- Sys.which("python3")
12+
if (python == "") { # nocov
13+
python <- Sys.which("python")
14+
if (python == "") {
15+
stop("Python not found in PATH. Please ensure Python is installed.")
16+
}
17+
}
18+
return(python)
19+
}
20+
21+
22+
#' Install ACRO in a conda environment
23+
#'
24+
#' @param envname Name of the conda environment
25+
install_conda <- function(envname) { # nocov
26+
if (!reticulate::condaenv_exists(envname = envname, conda = "auto")) {
27+
reticulate::conda_create(envname = envname, python_version = "3.12", channel = ch)
28+
reticulate::conda_install(envname = envname, packages = acro_pkg, channel = ch)
29+
}
30+
}
31+
32+
33+
#' Initialise ACRO in a Python virtual environment
34+
#'
35+
#' @param envname Name of the virtual environment
36+
install_venv <- function(envname = acro_venv) {
37+
if (!reticulate::virtualenv_exists(envname)) {
38+
python <- get_python()
39+
40+
reticulate::virtualenv_create(
41+
envname = envname,
42+
python = python,
43+
force = TRUE,
44+
packages = NULL
45+
)
46+
47+
reticulate::py_install(acro_pkg, envname = envname)
48+
}
49+
}
50+
51+
152
#' Initialise an ACRO object
253
#'
354
#' @param suppress Whether to automatically apply suppression.
55+
#' @param envname Name of the Python environment to use.
56+
#' @param use_conda Whether to use a Conda environment (`TRUE`) or venv (`FALSE`).
457
#'
5-
#' @return No return value, called for side effects
58+
#' @return Invisibly returns the ACRO object, which is used internally.
659
#' @export
60+
acro_init <- function(suppress = FALSE, envname = acro_venv, use_conda = FALSE) {
61+
# initialise the environment
62+
if (!reticulate::py_available(initialize = FALSE)) {
63+
if (use_conda) { # nocov
64+
install_conda(envname)
65+
reticulate::use_condaenv(envname, required = TRUE)
66+
} else {
67+
install_venv(envname)
68+
reticulate::use_virtualenv(envname, required = TRUE)
69+
}
70+
}
771

8-
acro_init <- function(suppress = FALSE) {
9-
create_virtualenv()
72+
# import the acro package and instantiate an object
1073
acro <- reticulate::import("acro", delay_load = TRUE)
1174
acroEnv$ac <- acro$ACRO(suppress = suppress)
75+
76+
invisible(acroEnv$ac)
1277
}

R/create_virtualenv.R

Lines changed: 0 additions & 55 deletions
This file was deleted.

man/acro_init.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_virtualenv.Rd

Lines changed: 0 additions & 17 deletions
This file was deleted.

man/get_python.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/install_acro.Rd

Lines changed: 0 additions & 21 deletions
This file was deleted.

man/install_conda.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/install_venv.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-create_virtualenv.R

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/testthat/test-install_acro.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test_that("install_acro installs 'acro' in the specified environment", {
44
test_envname <- "test-acro-env"
55

66
# Run the install_acro function
7-
install_acro(envname = test_envname)
7+
install_venv(envname = test_envname)
88

99
# Check if 'acro' is installed in the test environment
1010
is_installed <- reticulate::py_module_available("acro")

0 commit comments

Comments
 (0)