From 2ab150d38e8be924ca41d9da01fa29b9878bbff2 Mon Sep 17 00:00:00 2001 From: "Andrew G. Brown" Date: Mon, 20 Nov 2023 21:09:45 -0800 Subject: [PATCH 1/5] Add `install_rosetta(system=)` argument for #10 --- R/install_rosetta.R | 16 ++++++++++++---- man/install_rosetta.Rd | 3 +++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/R/install_rosetta.R b/R/install_rosetta.R index bcd92c2..b50a964 100644 --- a/R/install_rosetta.R +++ b/R/install_rosetta.R @@ -6,6 +6,7 @@ #' @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 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. #' @@ -17,6 +18,7 @@ install_rosetta <- function(envname = NULL, method = "auto", conda = "auto", pip = TRUE, + system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path")) { # use heuristics to find python executable @@ -24,11 +26,17 @@ 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 (isFALSE(system)) { + # get rosetta-soil (and numpy if needed) + try(reticulate::py_install("rosetta-soil", envname = envname, method = method, conda = conda, pip = pip), + silent = TRUE) + } else { + p <- Sys.which("python") + reticulate::use_python(p, required = TRUE) + system(paste(shQuote(p), "-m pip install --upgrade --user rosetta-soil")) + } - # 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..657a9ee 100644 --- a/man/install_rosetta.Rd +++ b/man/install_rosetta.Rd @@ -9,6 +9,7 @@ install_rosetta( method = "auto", conda = "auto", pip = TRUE, + system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path") ) } @@ -21,6 +22,8 @@ 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{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{ From 177d433d463b61372414a84fae340d86182171d7 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 21 Nov 2023 09:40:58 -0800 Subject: [PATCH 2/5] install_rosetta: Use `system=TRUE` in tests --- tests/testthat/test-install_rosetta.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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") From d1a513799ca0ad72a39906c07860a3a9b647c008 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 21 Nov 2023 13:07:23 -0800 Subject: [PATCH 3/5] Fix for multiple python.exe detected when setting `rosettaPTF.arcpy_path` --- R/find_python.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 83a4373b075a6362153014a3a09f627e4c3efaa6 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Tue, 21 Nov 2023 13:10:22 -0800 Subject: [PATCH 4/5] Add option (and warning statement") about installing to user site packages with `--user` flag for #10 - this is one of those "necessary footguns" --- R/install_rosetta.R | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/R/install_rosetta.R b/R/install_rosetta.R index b50a964..8f7f6fd 100644 --- a/R/install_rosetta.R +++ b/R/install_rosetta.R @@ -7,7 +7,9 @@ #' @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 @@ -18,6 +20,7 @@ install_rosetta <- function(envname = NULL, method = "auto", conda = "auto", pip = TRUE, + user = FALSE, system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path")) { @@ -26,14 +29,20 @@ install_rosetta <- function(envname = NULL, find_python(envname = envname, arcpy_path = arcpy_path) } + 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), - silent = TRUE) + try(reticulate::py_install("rosetta-soil", envname = envname, method = method, + conda = conda, pip = pip, pip_options = ifelse(user, "--user", "")), silent = TRUE) } else { - p <- Sys.which("python") reticulate::use_python(p, required = TRUE) - system(paste(shQuote(p), "-m pip install --upgrade --user rosetta-soil")) + 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) From 01894cba8c5ed478faadc84d9e40b8eb625c6c53 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Wed, 27 Dec 2023 11:23:25 -0800 Subject: [PATCH 5/5] docs --- man/install_rosetta.Rd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/man/install_rosetta.Rd b/man/install_rosetta.Rd index 657a9ee..18bb5a5 100644 --- a/man/install_rosetta.Rd +++ b/man/install_rosetta.Rd @@ -9,6 +9,7 @@ install_rosetta( method = "auto", conda = "auto", pip = TRUE, + user = FALSE, system = FALSE, arcpy_path = getOption("rosettaPTF.arcpy_path") ) @@ -22,6 +23,8 @@ 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.}