From 326ebde3ace0d686fcef632b9ab2a5582e0f2724 Mon Sep 17 00:00:00 2001 From: TomKellyGenetics Date: Thu, 28 Apr 2022 23:50:46 +0900 Subject: [PATCH] updates conda environment in interactive sessions only for compliance to CRAN checks --- .Rbuildignore | 1 + DESCRIPTION | 4 +- NEWS.md | 6 +- R/leiden.R | 222 ++++++++++++++++++++++++++--------------------- cran-comments.md | 2 +- 5 files changed, 129 insertions(+), 106 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 8f5370e..09b70c6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -46,3 +46,4 @@ images ^vignettes/bash_py_time$ ^revdep$ ^aarhus_mplex.rda$ +^CRAN-SUBMISSION$ diff --git a/DESCRIPTION b/DESCRIPTION index c81a1d5..c9e5165 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: leiden Type: Package Title: R Implementation of Leiden Clustering Algorithm Version: 0.3.10 -Date: 2021-07-27 +Date: 2022-04-25 Authors@R: c(person("S. Thomas", "Kelly", email = "tomkellygenetics@gmail.com", role = c("aut", "cre", "trl")), person("Vincent A.", "Traag", email = "v.a.traag@cwts.leidenuniv.nl", role = c("com"))) Description: Implements the 'Python leidenalg' module to be called in R. @@ -19,7 +19,7 @@ Imports: igraph Encoding: UTF-8 LazyData: false -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 Suggests: bipartite, covr, diff --git a/NEWS.md b/NEWS.md index bec711b..0183179 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,10 @@ # leiden 0.3.10 -* removes limitation on number of cells (disables scientific notation within function call) +* removes limitation on number of cells (disables scientific notation within function call): resolves #12 -* resolves conflict between base and r-reticulate conda environments on loading +* resolves conflict between base and r-reticulate conda environments on loading: resolves #20 + +* updates conda environment in interactive sessions only for compliance to CRAN checks * resolves formatting error in Rmarkdown vignettes (https://github.com/yihui/knitr/issues/2057) diff --git a/R/leiden.R b/R/leiden.R index b6ae1b6..a4de2cc 100644 --- a/R/leiden.R +++ b/R/leiden.R @@ -236,26 +236,26 @@ leiden.Matrix <- function(object, ##' @importClassesFrom Matrix dgCMatrix dgeMatrix ##' @export leiden.list <- function(object, - partition_type = c( - 'RBConfigurationVertexPartition', - 'ModularityVertexPartition', - 'RBERVertexPartition', - 'CPMVertexPartition', - 'MutableVertexPartition', - 'SignificanceVertexPartition', - 'SurpriseVertexPartition', - 'ModularityVertexPartition.Bipartite', - 'CPMVertexPartition.Bipartite' - ), - initial_membership = NULL, - weights = NULL, - node_sizes = NULL, - resolution_parameter = 1, - seed = NULL, - n_iterations = 2L, - max_comm_size = 0L, - degree_as_node_size = FALSE, - laplacian = FALSE + partition_type = c( + 'RBConfigurationVertexPartition', + 'ModularityVertexPartition', + 'RBERVertexPartition', + 'CPMVertexPartition', + 'MutableVertexPartition', + 'SignificanceVertexPartition', + 'SurpriseVertexPartition', + 'ModularityVertexPartition.Bipartite', + 'CPMVertexPartition.Bipartite' + ), + initial_membership = NULL, + weights = NULL, + node_sizes = NULL, + resolution_parameter = 1, + seed = NULL, + n_iterations = 2L, + max_comm_size = 0L, + degree_as_node_size = FALSE, + laplacian = FALSE ) { # disable printing numerals in scientific notation oo <- options(scipen = 100000000000) @@ -268,15 +268,15 @@ leiden.list <- function(object, if(length(object) == 1 || is.igraph(object)){ if(!is.igraph(object) && is.list(object)) object <- object[[1]] partition <- leiden.igraph(object, - partition_type = partition_type, - weights = weights, - node_sizes = node_sizes, - resolution_parameter = resolution_parameter, - seed = seed, - n_iterations = n_iterations, - max_comm_size = max_comm_size, - degree_as_node_size = degree_as_node_size, - laplacian = laplacian + partition_type = partition_type, + weights = weights, + node_sizes = node_sizes, + resolution_parameter = resolution_parameter, + seed = seed, + n_iterations = n_iterations, + max_comm_size = max_comm_size, + degree_as_node_size = degree_as_node_size, + laplacian = laplacian ) } else{ @@ -307,14 +307,14 @@ leiden.list <- function(object, #compute partitions with reticulate partition <- find_partition_multiplex(py_list, partition_type = partition_type, - initial_membership = initial_membership, - weights = weights, - node_sizes = node_sizes, - resolution_parameter = resolution_parameter, - seed = seed, - n_iterations = n_iterations, - max_comm_size = max_comm_size, - degree_as_node_size = degree_as_node_size + initial_membership = initial_membership, + weights = weights, + node_sizes = node_sizes, + resolution_parameter = resolution_parameter, + seed = seed, + n_iterations = n_iterations, + max_comm_size = max_comm_size, + degree_as_node_size = degree_as_node_size ) } partition @@ -445,13 +445,23 @@ pd <<- NULL is.reticulate.env <- any(grepl("r-reticulate/bin", reticulate::conda_list()$python)) # create conda env if no base image found if(!(is.reticulate.env)){ - reticulate::miniconda_update() - reticulate::conda_create(envname = "r-reticulate") - reticulate::conda_install(envname = "r-reticulate", packages = "conda") + if(interactive()){ + install.deps <- readline("create conda environment (yes/no)?") + packageStartupMessage(install.deps) + } else { + packageStartupMessage("create conda environment (yes/no)?") + install.deps <- "no (use interactive mode)" + packageStartupMessage("no (use interactive mode)") + } + if(install.deps == "yes" || install.deps == "y"){ + reticulate::miniconda_update() + reticulate::conda_create(envname = "r-reticulate") + reticulate::conda_install(envname = "r-reticulate", packages = "conda") + } } # use "r-reticulate" or "base" image (which ever is used by reticulate if installed already) reticulate.env <- reticulate::conda_list()$name[grep("r-reticulate/bin/python", reticulate::conda_list()$python)][1] - print(paste(c("using environment:", reticulate.env), collapse = " ")) + packageStartupMessage(paste(c("using environment:", reticulate.env), collapse = " ")) suppressWarnings(suppressMessages(reticulate::use_python(reticulate::conda_python()))) suppressWarnings(suppressMessages(reticulate::use_condaenv(reticulate.env))) }, error = function(e){ @@ -465,74 +475,84 @@ pd <<- NULL tryCatch({ is.reticulate.env <- any(grepl("r-reticulate/bin", reticulate::conda_list()$python)) if(reticulate::py_available() || is.reticulate.env ){ - reticulate.env <- reticulate::conda_list()$name[grep("r-reticulate/bin/python", reticulate::conda_list()$python)][1] - print(paste(c("using environment:", reticulate.env), collapse = " ")) - install_python_modules <- function(method = "auto", conda = "auto") { - if(!is.null(reticulate::conda_binary())){ - reticulate::use_python(reticulate::conda_python()) - if(!(is.reticulate.env)){ - reticulate::conda_create(envname = reticulate.env) - if(!reticulate::py_module_available("conda")) reticulate::conda_install(envname = reticulate.env, packages = "conda") - } - suppressWarnings(suppressMessages(reticulate::use_condaenv(reticulate.env))) - if(.Platform$OS.type == "windows"){ - utils::install.packages("devtools", quiet = TRUE) - devtools::install_github("rstudio/reticulate", ref = "86ebb56", quiet = TRUE) - if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "numpy"))) - if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "pandas"))) - if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "python-igraph"))) - if(!reticulate::py_module_available("mkl")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "mkl", channel = "intel"))) - if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "umap-learn", channel = "conda-forge"))) - if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "leidenalg", channel = "conda-forge"))) - install.packages("reticulate", quiet = TRUE) - if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "leidenalg"))) #, channel = "conda-forge") - utils::install.packages("reticulate", quiet = TRUE) + if(interactive()){ + install.deps <- readline("install dependencies (yes/no)?") + packageStartupMessage(install.deps) + } else { + packageStartupMessage("install dependencies (yes/no)?") + install.deps <- "no (use interactive mode)" + packageStartupMessage("no (use interactive mode)") + } + if(install.deps == "yes" || install.deps == "y"){ + reticulate.env <- reticulate::conda_list()$name[grep("r-reticulate/bin/python", reticulate::conda_list()$python)][1] + packageStartupMessage(paste(c("using environment:", reticulate.env), collapse = " ")) + install_python_modules <- function(method = "auto", conda = "auto") { + if(!is.null(reticulate::conda_binary())){ + reticulate::use_python(reticulate::conda_python()) + if(!(is.reticulate.env)){ + reticulate::conda_create(envname = reticulate.env) + if(!reticulate::py_module_available("conda")) reticulate::conda_install(envname = reticulate.env, packages = "conda") + } + suppressWarnings(suppressMessages(reticulate::use_condaenv(reticulate.env))) + if(.Platform$OS.type == "windows"){ + utils::install.packages("devtools", quiet = TRUE) + devtools::install_github("rstudio/reticulate", ref = "86ebb56", quiet = TRUE) + if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "numpy"))) + if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "pandas"))) + if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "python-igraph"))) + if(!reticulate::py_module_available("mkl")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "mkl", channel = "intel"))) + if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "umap-learn", channel = "conda-forge"))) + if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "leidenalg", channel = "conda-forge"))) + install.packages("reticulate", quiet = TRUE) + if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(envname = reticulate.env, packages = "leidenalg"))) #, channel = "conda-forge") + utils::install.packages("reticulate", quiet = TRUE) + } else { + if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "numpy"))) + if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "pandas"))) + if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "python-igraph"))) + if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "umap-learn", forge = TRUE))) + if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "leidenalg", forge = TRUE))) + #Sys.setenv(PATH = paste0(strsplit(reticulate::py_config()$pythonhome, ":")[[1]][1], "/bin:$PATH")) + Sys.setenv(RETICULATE_PYTHON = reticulate::conda_python()) + } } else { - if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "numpy"))) - if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "pandas"))) - if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "python-igraph"))) - if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "umap-learn", forge = TRUE))) - if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::conda_install(reticulate.env, "leidenalg", forge = TRUE))) + # shell <- strsplit(Sys.getenv("SHELL"), "/")[[1]] + # shell <- shell[length(shell)] + # eval(parse(text = paste0(c('system("conda init ', shell, '")'), collapse = ""))) + # eval(parse(text = paste0(c('system("source ~/.', shell, 'rc")'), collapse = ""))) + # shell <- as.list(system("echo $0")) + # if(shell == sh) shell <- "bash" + # system("conda init") + # eval(parse(text = paste0(c('system("source ~/.', shell, '_profile")'), collapse = ""))) + # system("conda init") + # system("conda activate r-reticulate") + if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::py_install("numpy"))) + if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::py_install("pandas"))) + if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::py_install("python-igraph", method = method, conda = conda))) + if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::py_install("umap-learn"))) + if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::py_install("leidenalg", method = method, conda = conda, forge = TRUE))) #Sys.setenv(PATH = paste0(strsplit(reticulate::py_config()$pythonhome, ":")[[1]][1], "/bin:$PATH")) - Sys.setenv(RETICULATE_PYTHON = reticulate::conda_python()) + Sys.setenv(RETICULATE_PYTHON = reticulate::py_config()$python) } - } else { - # shell <- strsplit(Sys.getenv("SHELL"), "/")[[1]] - # shell <- shell[length(shell)] - # eval(parse(text = paste0(c('system("conda init ', shell, '")'), collapse = ""))) - # eval(parse(text = paste0(c('system("source ~/.', shell, 'rc")'), collapse = ""))) - # shell <- as.list(system("echo $0")) - # if(shell == sh) shell <- "bash" - # system("conda init") - # eval(parse(text = paste0(c('system("source ~/.', shell, '_profile")'), collapse = ""))) - # system("conda init") - # system("conda activate r-reticulate") - if(!reticulate::py_module_available("numpy")) suppressWarnings(suppressMessages(reticulate::py_install("numpy"))) - if(!reticulate::py_module_available("pandas")) suppressWarnings(suppressMessages(reticulate::py_install("pandas"))) - if(!reticulate::py_module_available("igraph")) suppressWarnings(suppressMessages(reticulate::py_install("python-igraph", method = method, conda = conda))) - if(!reticulate::py_module_available("umap")) suppressWarnings(suppressMessages(reticulate::py_install("umap-learn"))) - if(!reticulate::py_module_available("leidenalg")) suppressWarnings(suppressMessages(reticulate::py_install("leidenalg", method = method, conda = conda, forge = TRUE))) - #Sys.setenv(PATH = paste0(strsplit(reticulate::py_config()$pythonhome, ":")[[1]][1], "/bin:$PATH")) - Sys.setenv(RETICULATE_PYTHON = reticulate::py_config()$python) - } - } - quiet <- function(expr, all = TRUE) { - if (Sys.info()['sysname'] == "Windows") { - file <- "NUL" - } else { - file <- "/dev/null" } + quiet <- function(expr, all = TRUE) { + if (Sys.info()['sysname'] == "Windows") { + file <- "NUL" + } else { + file <- "/dev/null" + } - if (all) { - suppressWarnings(suppressMessages(suppressPackageStartupMessages( + if (all) { + suppressWarnings(suppressMessages(suppressPackageStartupMessages( + capture.output(expr, file = file) + ))) + } else { capture.output(expr, file = file) - ))) - } else { - capture.output(expr, file = file) - } + } + } + quiet(install_python_modules()) } - quiet(install_python_modules()) } }, error = function(e){ packageStartupMessage("Unable to install python modules igraph and leidenalg") diff --git a/cran-comments.md b/cran-comments.md index 1514c26..efe6062 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -5,7 +5,7 @@ * rhub (release) Ubuntu Linux 16.04 LTS, R-release, GCC * Fedora (devel) Linux, R-devel, clang, gfortran * MacOS 10.14.6 R 3.6.1 -* MacOS 10.15.7 R 4.1.0 +* MacOS 10.15.7 R 4.2.0 ## R CMD check results