R/conda_export.R

Defines functions conda_export

Documented in conda_export

#' conda_export
#'
#' @param condaenv name or path of conda environment to export
#' @param export_dir export directory (default: tempdir())
#'
#' @return creates  export_dir/environment_condaenv.yml and returns path
#' @export
#' @importFrom reticulate miniconda_path py_config use_miniconda
#'
conda_export <- function(condaenv, export_dir = tempdir()) {
  stopifnot(dir.exists(reticulate::miniconda_path()))
  stopifnot(dir.exists(export_dir))
  reticulate::use_miniconda(condaenv = condaenv, required = TRUE)
  pyconfig <- reticulate::py_config()

  ## on windows: (try this: https://github.com/rstudio/reticulate/issues/367#issuecomment-432920802)
  if(.Platform$OS.type == "windows") {
    Sys.setenv(PATH = paste(PATH = pyconfig$pythonpath,
                            Sys.getenv()["PATH"], sep=";")
    )
  }

  path_env_yml <- file.path(export_dir, paste0("environment_", condaenv, ".yml"))

  sys_name <- as.character(Sys.info()['sysname'])

  conda_bat <- miniconda_conda(path = reticulate::miniconda_path())
  stopifnot(file.exists(conda_bat))

  cmds <- sprintf("%s activate %s && %s env export > %s",
                  conda_bat,
                  condaenv,
                  conda_bat,
                  path_env_yml)

   if(sys_name == "Windows") {
    shell(cmd = cmds)
  } else {
    system2(command = cmds)
  }

  stopifnot(file.exists(path_env_yml))
  path_env_yml
}
KWB-R/kwb.python documentation built on May 30, 2022, 9:43 a.m.