Skip to content

Commit

Permalink
Create an dedicated conda environment for deepforest
Browse files Browse the repository at this point in the history
Following "Pit of Success" method from:
https://rstudio.github.io/reticulate/articles/python_dependencies.html

Heavily influenced by tensorflow::install_tensorflow :
https://github.com/rstudio/tensorflow/blob/main/R/install.R
  • Loading branch information
ethanwhite committed Jul 22, 2024
1 parent 12d9053 commit cfd1354
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ URL: https://github.com/weecology/DeepForest/ (website),
Depends:
R (>= 3.4.0)
Imports:
reticulate (>= 1.16)
reticulate (>= 1.16),
rstudioapi (>= 0.7)
Suggests:
testthat (>= 1.0.0),
devtools,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(df_model)
export(get_data)
export(install_deepforest)
importFrom(reticulate,conda_binary)
importFrom(reticulate,conda_remove)
importFrom(reticulate,import)
importFrom(reticulate,install_miniconda)
Expand Down
52 changes: 36 additions & 16 deletions R/deepforestr.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
#' Install the DeepForest Python package
#'
#' The package will be installed into it's own conda environment 'r-deepforest'
#' by default. If conda is already installed on the system the existing version
#' will be used. If it is not installed then it will be automatically installed.
#'
#' @param envname Name of conda environment for installing deepforest
#'
#' @param restart_session Restart R session after installing (note this will
#' only occur within RStudio).
#'
#' @param new_env If `TRUE`, any existing conda environment specified by
#' `envname` is deleted first.
#'
#' @param ... other arguments passed to [`reticulate::conda_install()`]
#'
#' @examples
#' \dontrun{
#' deepforestr::install_deepforest()}
#'
#' @importFrom reticulate install_miniconda py_install conda_remove
#'
#' @importFrom reticulate install_miniconda py_install conda_remove conda_binary
#' @export
install_deepforest <- function() {
miniconda_path = reticulate::miniconda_path()
if (!dir.exists(miniconda_path)) {
reticulate::install_miniconda()
install_deepforest <- function(envname = "r-deepforest",
restart_session = TRUE,
new_env = identical(envname, "r-deepforest"),
...) {
if (is.null(tryCatch(conda_binary(), error = function(e) NULL))) {
install_miniconda()
} else {
print(sprintf("Using existing miniconda install at %s", miniconda_path))
print(sprintf("Using existing miniconda install at %s", conda_binary()))
}
reticulate::py_install(c("gdal", "rasterio", "fiona"), method = "conda")
#if (reticulate::py_module_available("mkl")) {
deps <- c("gdal", "rasterio", "fiona")
py_install(deps, envname = envname, method = "conda", ...)
#if (py_module_available("mkl")) {
# Remove package that has caused conflicts on Windows due to double install
# The correct version of mkl will be installed with deepforest (below)
# on systems where it is needed
# reticulate::conda_remove("r-reticulate", packages = c("mkl"))
# reticulate::conda_remove(envname, packages = c("mkl"))
#}
reticulate::py_install("DeepForest", method = "conda", pip = TRUE)
py_install("DeepForest", envname = envname, method = "conda", pip = TRUE)

if (restart_session && rstudioapi::hasFun("restartSession")){
rstudioapi::restartSession()
}

invisible(NULL)
}

#' Get example data
Expand Down Expand Up @@ -54,10 +78,6 @@ df_model <- function() {
deepforest <- NULL

.onLoad <- function(libname, pkgname) {
## assignment in parent environment!
try({
deepforest <<- reticulate::import("deepforest", delay_load = TRUE)
# Disable due to failure to test on win cran dev platform
# check_deepforest_availability()
}, silent = TRUE)
reticulate::use_condaenv("r-deepforest", required = FALSE)
deepforest <<- reticulate::import("deepforest", delay_load = TRUE)
}
23 changes: 21 additions & 2 deletions man/install_deepforest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfd1354

Please sign in to comment.