Skip to content

Commit 9037a18

Browse files
committed
feat: add support for conda environments
1 parent ddb5da0 commit 9037a18

10 files changed

Lines changed: 121 additions & 117 deletions

R/acro_init.R

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,81 @@
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+
156
#' Initialise an ACRO object
257
#'
358
#' @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`).
461
#'
5-
#' @return No return value, called for side effects
62+
#' @return Invisibly returns the ACRO object, which is used internally.
663
#' @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+
}
775

8-
acro_init <- function(suppress = FALSE) {
9-
create_virtualenv()
76+
# import the acro package and instantiate an object
1077
acro <- reticulate::import("acro", delay_load = TRUE)
1178
acroEnv$ac <- acro$ACRO(suppress = suppress)
79+
80+
invisible(acroEnv$ac)
1281
}

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)