Skip to content

Commit a84be35

Browse files
committed
feat: re-imagined configure_python_environment
to configure a dedicated chromConverter virtual environment or conda environment (though this is no longer required with the latest versions of reticulate).
1 parent a9eea01 commit a84be35

File tree

2 files changed

+82
-51
lines changed

2 files changed

+82
-51
lines changed

R/utils.R

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ check_data_format <- function(data_format, format_out){
2323
#' Convert chromatogram format
2424
#' @author Ethan Bass
2525
#' @noRd
26-
convert_chrom_format <- function(x, format_out){
26+
convert_chrom_format <- function(x, format_out, data_format=NULL){
27+
if (is.null(data_format)){
28+
data_format <- attr(x, "data_format")
29+
}
2730
if (inherits(x, format_out)){
2831
return(x)
2932
} else if (format_out == "matrix"){
3033
return(as.matrix(x))
3134
} else if (format_out == "data.frame"){
3235
return(as.data.frame(x))
3336
} else if (format_out == "data.table"){
34-
return(data.table::as.data.table(x))
37+
return(data.table::as.data.table(x, keep.rownames = ifelse(data_format == "wide",
38+
yes = "rt", no = FALSE))
39+
)
3540
}
3641
}
3742

@@ -363,54 +368,63 @@ split_at <- function(x, pos) unname(split(x, cumsum(seq_along(x) %in% pos)))
363368

364369
#' Configure python environment
365370
#'
366-
#' Configures reticulate environment for parsers that have python dependencies.
367-
#' Deprecated as this should no longer be necessary with reticulate \code{v1.41.0}.
371+
#' Configures python virtual environment or conda environment for parsers that
372+
#' have python dependencies, according to the value of \code{what}. While this
373+
#' should not be necessary in most cases starting with reticulate \code{v1.41.0},
374+
#' this function can be used to create a dedicated chromConverter environment.
368375
#'
369376
#' @name configure_python_environment
370-
#' @param parser Either \code{aston}, \code{rainbow}, or \code{olefile} (for
371-
#' \code{read_shimadzu_lcd}).
372-
#' @param return_boolean Logical. Whether to return a Boolean value indicating
373-
#' if the chromConverter environment is correctly configured.
374-
#' @return If \code{return_boolean} is \code{TRUE}, returns a Boolean value
375-
#' indicating whether the chromConverter environment is configured correctly.
376-
#' Otherwise, there is no return value.
377+
#' @param envname The name of, or path to, a Python virtual environment.
378+
#' @param what What kind of virtual environment to create. A python virtual
379+
#' environment (\code{"venv"}) or a conda environment (\code{"conda"}).
380+
#' @param python Argument to \code{reticulate::virtualenv_create}, specifying
381+
#' the path to a Python interpreter.
382+
#' @param ... Additional arguments to [reticulate::virtualenv_create] or
383+
#' [reticulate::conda_create] according to the value of \code{what}.
384+
#' @return There is no return value.
385+
#' @section Side effects:
386+
#' Creates and configures either a python virtual environment or conda
387+
#' environment (according to the value of \code{what}) with all the packages
388+
#' required for running chromConverter.
377389
#' @author Ethan Bass
378390
#' @import reticulate
379391
#' @keywords internal
380392
#' @export
393+
#' @md
381394

382-
configure_python_environment <- function(parser = "all", return_boolean = FALSE){
383-
warning("This function is deprecated as of chromConverter v0.7.4 as
384-
miniconda should no longer be necessary to load python dependencies. Continue anyway...? (y/n)",
385-
immediate. = TRUE)
386-
continue <- readline()
387-
if (continue %in% c('y', "Y", "YES", "yes", "Yes")){
388-
parser <- match.arg(tolower(parser), c("all", "aston", "olefile", "rainbow"))
389-
install <- FALSE
390-
if (!dir.exists(reticulate::miniconda_path())){
391-
install <- readline(sprintf("It is recommended to install miniconda in your R library to use %s parsers. Install miniconda now? (y/n)",
392-
ifelse(parser == "all", "python-based", parser)))
393-
if (install %in% c('y', "Y", "YES", "yes", "Yes")){
394-
reticulate::install_miniconda()
395-
}
396-
}
397-
env <- reticulate::configure_environment("chromConverter")
398-
if (!env){
399-
reqs <- get_parser_reqs(parser)
400-
reqs_available <- sapply(reqs, reticulate::py_module_available)
401-
if (!all(reqs_available)){
402-
reticulate::conda_install(envname = "chromConverter",
403-
reqs[which(!reqs_available)], pip = TRUE)
404-
}
405-
}
406-
assign_fn <- switch(parser, aston = assign_trace_file(),
407-
rainbow = assign_rb_read(),
408-
function(){})
409-
assign_fn()
410-
if (return_boolean){
411-
env
395+
configure_python_environment <- function(what = c("venv", "conda"),
396+
envname = "chromConverter",
397+
python = reticulate::virtualenv_starter(),
398+
...){
399+
what <- match.arg(what, c("venv", "conda"))
400+
packages <- c("Aston", "olefile", "pandas", "rainbow-api", "scipy")
401+
install <- FALSE
402+
if (!dir.exists(reticulate::miniconda_path())){
403+
install <- readline(sprintf("It is recommended to install miniconda in your R library to use %s parsers. Install miniconda now? (y/n)"))
404+
if (install %in% c('y', "Y", "YES", "yes", "Yes")){
405+
reticulate::install_miniconda()
412406
}
413407
}
408+
check_name <- switch(what, "venv" = reticulate::virtualenv_exists,
409+
"conda" = reticulate::condaenv_exists)
410+
exists <- check_name(envname)
411+
if (exists){
412+
stop(sprintf('The %s environment, "%s" already exists. To create a new environment,
413+
please remove the existing environment first using `%s("%s")`.',
414+
switch(what,"venv" = "virtual", "conda" = "conda"), envname,
415+
switch(what, "venv" = "reticulate::virtualenv_remove",
416+
"conda" = "reticulate::conda_remove"),
417+
envname))
418+
}
419+
if (what == "venv"){
420+
reticulate::virtualenv_create(envname = envname, packages = packages,
421+
python = python, ...)
422+
} else if (what == "conda"){
423+
reticulate::conda_create(envname = envname,
424+
packages = c("olefile", "pandas", "scipy"), ...)
425+
reticulate::conda_install(envname = envname,
426+
packages=c("Aston", "rainbow-api"), pip = TRUE)
427+
}
414428
}
415429

416430
#' Get required python packages for a parser

man/configure_python_environment.Rd

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)