File: useBasiliskEnv.R

package info (click to toggle)
r-bioc-basilisk 1.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: python: 12; sh: 4; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#' Use \pkg{basilisk} environments
#'
#' Use \pkg{basilisk} environments for isolated execution of Python code with appropriate versions of all Python packages.
#' 
#' @param envpath String containing the path to the \pkg{basilisk} environment to use. 
#' @param full.activation Logical scalar, see \code{\link{activateEnvironment}} for details.
#' 
#' @return 
#' The function will attempt to load the specified \pkg{basilisk} environment into the R session,
#' possibly with the modification of some environment variables (see Details).
#' A \code{NULL} is invisibly returned.
#'
#' @details
#' It is unlikely that developers should ever need to call \code{\link{useBasiliskEnv}} directly.
#' Rather, this interaction should be automatically handled by \code{\link{basiliskStart}}.
#' 
#' This function will modify a suite of environment variables as a side effect
#' - see \dQuote{Persistence of environment variables} in \code{?\link{basiliskStart}} for the rationale.
#'
#' @author Aaron Lun
#' 
#' @examples
#' if (.Platform$OS.type != "windows") {
#'  \dontshow{basilisk.utils::installConda()}
#' 
#'  tmploc <- file.path(tempdir(), "my_package_A")
#'  if (!file.exists(tmploc)) {
#'      setupBasiliskEnv(tmploc, c('pandas==1.4.3'))
#'  }
#' 
#'  # This may or may not work, depending on whether a Python instance
#'  # has already been loaded into this R session.
#'  try(useBasiliskEnv(tmploc))
#' 
#'  # This will definitely not work, as the available Python is already set.
#'  baseloc <- basilisk.utils::getCondaDir()
#'  status <- try(useBasiliskEnv(baseloc))
#' 
#'  # ... except on Windows, which somehow avoids tripping the error.
#'  stopifnot(is(status, "try-error") || basilisk.utils::isWindows())
#' }
#'
#' @seealso
#' \code{\link{basiliskStart}}, for how these \pkg{basilisk} environments should be used.
#'
#' @export
#' @import basilisk.utils
#' @importFrom reticulate use_condaenv py_config
useBasiliskEnv <- function(envpath, full.activation=NA) {
    envpath <- normalizePath(envpath, mustWork=TRUE)

    activateEnvironment(envpath, full.activation=full.activation)
    reticulate::use_condaenv(envpath, required=TRUE)

    # use_condaenv doesn't actually cause Python to be loaded immediately, 
    # so we force the issue to seal the deal.
    reticulate::py_config() 

    invisible(NULL)
}

#' @importFrom reticulate py_config 
.same_as_loaded <- function(envpath) 
# Checking whether we're the same as the existing python instance,
# which would indicate that we correctly loaded `envpath`.
{
    expected <- normalizePath(getPythonBinary(envpath)) 
    actual <- normalizePath(py_config()$python)
    identical(expected, actual)
}