Skip to content

Instantly share code, notes, and snippets.

@abalter
Created January 8, 2019 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abalter/e104c192d7a28a88685288befae3277f to your computer and use it in GitHub Desktop.
Save abalter/e104c192d7a28a88685288befae3277f to your computer and use it in GitHub Desktop.
A function to enable using conda seamlessly with R
  • Install EVERYTHING you can with conda
  • Install things you can with:
    • install.packages(, lib="")
    • install.github(, lib="")
    • biocLite(, lib="")
  • Make separate local libs for different R versions
  • After installing a package to a local lib, look there and see if there are dependencies you could have installed with conda.
  • Or, head it off by looking at the dependencies in CRAN or Bioconductor
options(repos=structure(c(CRAN="https://ftp.osuosl.org/pub/cran/")))
startCondaEnv = function(env_name, lib='/home/balter/R')
{
cat("pointing to conda env:", env_name, "and lib location", lib, "\n")
r_lib_path = lib
if (env_name == "" || env_name == "base")
{
#print("using default base env")
conda_lib_path = file.path('/home/balter/conda/lib/R/library')
}else
{
conda_lib_path = file.path('/home/balter/conda/envs', env_name, 'lib/R/library')
}
#cat('conda_lib_path: ', conda_lib_path, '\n')
.libPaths(new=c(conda_lib_path, r_lib_path))
#cat(".libPaths():\n")
print(.libPaths())
}
current_conda_env = Sys.getenv('CONDA_DEFAULT_ENV')
cat('current_conda_env:', current_conda_env, '\n')
current_conda_prefix = Sys.getenv('CONDA_PREFIX')
cat('current_conda_prefix:', current_conda_prefix, '\n')
if (current_conda_env != "")
{
r_version = R.Version()
r_version = paste0(r_version$major, '.', r_version$minor)
if (r_version == '3.5.1')
{
r_lib_path = '/home/balter/R35'
}else
if (r_version == '3.4.11')
{
r_lib_path = '/home/balter/R34'
}else
{
message("no compatible lib")
r_lib_path = ''
}
#cat("env: ", current_conda_env, ", prefix: ", current_conda_prefix, "\n")
conda_env_lib = file.path(current_conda_prefix,'lib/R/library')
startCondaEnv(current_conda_env, lib=r_lib_path)
}else
{
print("no conda env")
}
startCondaEnv = function(env_name, lib='/home/balter/R')
{
cat("pointing to conda env:", env_name, "and lib location", lib, "\n")
r_lib_path = lib
if (env_name == "" || env_name == "base")
{
#print("using default base env")
conda_lib_path = file.path('/home/balter/conda/lib/R/library')
}else
{
conda_lib_path = file.path('/home/balter/conda/envs', env_name, 'lib/R/library')
}
#cat('conda_lib_path: ', conda_lib_path, '\n')
.libPaths(new=c(conda_lib_path, r_lib_path))
#cat(".libPaths():\n")
print(.libPaths())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment