diff --git a/R/find_python.R b/R/find_python.R index 62ed4a5..27ebd9d 100644 --- a/R/find_python.R +++ b/R/find_python.R @@ -109,8 +109,8 @@ find_python <- function(envname = NULL, options(rosettaPTF.python_path = PYEXE_PATH) options(rosettaPTF.arcpy_path = arcpy_path) - subres - }, silent = TRUE) + subres[grep("envs/arcgispro-py3", subres, fixed = TRUE)] + }, silent = FALSE) # User can/should use regular reticulate methods for this diff --git a/R/install_rosetta.R b/R/install_rosetta.R index bcd92c2..8f7f6fd 100644 --- a/R/install_rosetta.R +++ b/R/install_rosetta.R @@ -6,7 +6,10 @@ #' @param method `"auto"`, `"virtualenv"`, or `"conda"`; Default: `"auto"` #' @param conda Default: `"auto"` #' @param pip _logical_. Use `pip` for package installation? Default: `TRUE`. This is only relevant when Conda environments are used, as otherwise packages will be installed from the Conda repositories. +#' @param system _logical_. Default: `FALSE`. If `TRUE`, try installing to system (user) site library with `system()` and set reticulate to use system Python. +#' @param user _logical_. Default: `FALSE`. Pass `--user` flag. This should only be done if other installation methods fail and it is impossible to use a virtual environment. #' @param arcpy_path Argument passed to `find_python()`. Path to ArcGIS Pro Python installation e.g. ``. Set as `NULL` (default) to prevent use of ArcGIS Pro instance. +#' #' @details From `reticulate::py_install()`: On Linux and OS X the "virtualenv" method will be used by default ("conda" will be used if virtualenv isn't available). On Windows, the "conda" method is always used. #' #' @export @@ -17,6 +20,8 @@ install_rosetta <- function(envname = NULL, method = "auto", conda = "auto", pip = TRUE, + user = FALSE, + system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path")) { # use heuristics to find python executable @@ -24,11 +29,23 @@ install_rosetta <- function(envname = NULL, find_python(envname = envname, arcpy_path = arcpy_path) } - # get rosetta-soil (and numpy if needed) - try(reticulate::py_install("rosetta-soil", envname = envname, method = method, conda = conda, pip = pip), - silent = TRUE) + if (!is.null(arcpy_path) && dir.exists(arcpy_path)) { + p <- find_python(envname = envname, arcpy_path = arcpy_path) + system <- TRUE + } else { + p <- Sys.which("python") + } + + if (isFALSE(system)) { + # get rosetta-soil (and numpy if needed) + try(reticulate::py_install("rosetta-soil", envname = envname, method = method, + conda = conda, pip = pip, pip_options = ifelse(user, "--user", "")), silent = TRUE) + } else { + reticulate::use_python(p, required = TRUE) + try(system(paste(shQuote(p), "-m pip install --upgrade --user rosetta-soil")), silent = TRUE) + } - # load modules globally in package (prevents having to reload rosettaPTF library in session) + # load modules globally in package (prevents having to reload rosettaPTF library in session) .loadModules() } diff --git a/man/install_rosetta.Rd b/man/install_rosetta.Rd index b0a4919..18bb5a5 100644 --- a/man/install_rosetta.Rd +++ b/man/install_rosetta.Rd @@ -9,6 +9,8 @@ install_rosetta( method = "auto", conda = "auto", pip = TRUE, + user = FALSE, + system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path") ) } @@ -21,6 +23,10 @@ install_rosetta( \item{pip}{\emph{logical}. Use \code{pip} for package installation? Default: \code{TRUE}. This is only relevant when Conda environments are used, as otherwise packages will be installed from the Conda repositories.} +\item{user}{\emph{logical}. Default: \code{FALSE}. Pass \code{--user} flag. This should only be done if other installation methods fail and it is impossible to use a virtual environment.} + +\item{system}{\emph{logical}. Default: \code{FALSE}. If \code{TRUE}, try installing to system (user) site library with \code{system()} and set reticulate to use system Python.} + \item{arcpy_path}{Argument passed to \code{find_python()}. Path to ArcGIS Pro Python installation e.g. ``. Set as \code{NULL} (default) to prevent use of ArcGIS Pro instance.} } \description{ diff --git a/tests/testthat/test-install_rosetta.R b/tests/testthat/test-install_rosetta.R index acf1041..e0722e9 100644 --- a/tests/testthat/test-install_rosetta.R +++ b/tests/testthat/test-install_rosetta.R @@ -8,7 +8,7 @@ test_that("rosetta-soil module can be installed", { cat("\n\n") # use pip (if available) or use ArcGIS Pro Conda environment (if available) - res <- try(install_rosetta(pip = TRUE, arcpy_path = "C:/Program Files/ArcGIS/Pro/bin/Python/")) + res <- try(install_rosetta(system = TRUE, arcpy_path = "C:/Program Files/ArcGIS/Pro/bin/Python/")) if (inherits(res, 'try-error')) skip("Unable to install")