├── .Rbuildignore ├── NAMESPACE ├── .gitignore ├── DESCRIPTION ├── RLinuxModules.Rproj ├── man ├── moduleInit.Rd └── module.Rd ├── README.md ├── LICENSE └── R ├── moduleInit.R └── module.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(module) 4 | export(moduleInit) 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | 5 | # Example code in package build process 6 | *-Ex.R 7 | 8 | # RStudio files 9 | .Rproj.user/ 10 | 11 | # produced vignettes 12 | vignettes/*.html 13 | vignettes/*.pdf 14 | .Rproj.user 15 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RLinuxModules 2 | Type: Package 3 | Title: Interface to the linux Environment Modules 4 | Version: 0.3 5 | Date: 2015-09-03 6 | Author: Lars Grønvold 7 | Maintainer: Lars Grønvold 8 | Description: Makes linux Environment Modules available in R 9 | License: MIT 10 | LazyData: TRUE 11 | RoxygenNote: 6.1.0 12 | -------------------------------------------------------------------------------- /RLinuxModules.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /man/moduleInit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/moduleInit.R 3 | \name{moduleInit} 4 | \alias{moduleInit} 5 | \title{Initialize Environment Modules interface} 6 | \usage{ 7 | moduleInit(version = "3.2.10", 8 | modulesHome = "/local/genome/Modules/3.2.10") 9 | } 10 | \arguments{ 11 | \item{version}{The version of the module system that is installed} 12 | 13 | \item{modulesHome}{Path to where the module system is installed} 14 | } 15 | \description{ 16 | Initialize linux Environment Modules. Must be called before using \code{\link{module}} 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RLinuxModules 2 | R package that makes linux [environment modules](http://modules.sourceforge.net/) available from R. 3 | 4 | > Note: Lmod environment module system have support for R so this package is not needed. 5 | 6 | ## installation 7 | ```r 8 | devtools::install_github("larsgr/RLinuxModules") 9 | ``` 10 | 11 | ## use example: 12 | ```r 13 | library(RLinuxModules) 14 | 15 | moduleInit( modulesHome = "yourpathToModulesEnvironment") 16 | 17 | module("load samtools") # loads samtools into the environment 18 | 19 | system("samtools") # samtools should now be available (if you have that module) 20 | ``` 21 | 22 | ## How it works 23 | The Modules Environment does not support R scripting but does support Python. This package works by using the python support and translating the python commands returned from *modulecmd python* into R commands. It has only been tested for version 3.2.10 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 larsgr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /man/module.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/module.R 3 | \name{module} 4 | \alias{module} 5 | \title{Linux Environment Modules interface} 6 | \usage{ 7 | module(Arguments) 8 | } 9 | \arguments{ 10 | \item{Arguments}{"[ switches ] [ subcommand ] [ subcommand-args ]" See examples.} 11 | } 12 | \value{ 13 | Output messages will be sent to stderr 14 | } 15 | \description{ 16 | Access the linux Environment Modules which provides for the dynamic modification of a user's 17 | environment via modulefiles. 18 | } 19 | \details{ 20 | Each modulefile contains the information needed to configure the shell for an application. 21 | Once the RLinuxModules package is initialized (with \code{\link{moduleInit}}), the environment can 22 | be modified on a per-module basis using the module command which interprets modulefiles. Typically 23 | modulefiles instruct the module command to alter or set shell environment variables such as PATH, 24 | MANPATH, etc. modulefiles may be shared by many users on a system and users may have their own 25 | collection to supplement or replace the shared modulefiles. 26 | } 27 | \examples{ 28 | module("avail") # shows available modules 29 | 30 | module("--help") # show available sub-commands and switches 31 | 32 | module("load samtools") # loads the module "samtools" 33 | system("which samtools") # check that samtools is loaded in the environment 34 | 35 | module("list") # list loaded modules 36 | 37 | module("unload samtools") # unload the samtools module 38 | 39 | module("load samtools/1.0") # loads a specific version of the module "samtools" 40 | system("which samtools") # check that the correct samtools is loaded in the environment 41 | 42 | } 43 | -------------------------------------------------------------------------------- /R/moduleInit.R: -------------------------------------------------------------------------------- 1 | #' Initialize Environment Modules interface 2 | #' 3 | #' Initialize linux Environment Modules. Must be called before using \code{\link{module}} 4 | #' 5 | #' @param version The version of the module system that is installed 6 | #' @param modulesHome Path to where the module system is installed 7 | #' 8 | #' 9 | #' @export 10 | moduleInit <- function( version = '3.2.10', 11 | modulesHome = '/local/genome/Modules/3.2.10'){ 12 | 13 | # Check if modulecmd exists in the 14 | if(!file.exists( file.path(modulesHome,"bin/modulecmd") )){ 15 | stop(file.path(modulesHome,"bin/modulecmd")," missing!\n", 16 | " Module environment init failed!" ) 17 | } 18 | 19 | 20 | if( is.na(Sys.getenv('MODULE_VERSION', unset = NA)) ){ 21 | Sys.setenv(MODULE_VERSION_STACK = version, 22 | MODULE_VERSION = version) 23 | } else { 24 | Sys.setenv(MODULE_VERSION_STACK = Sys.getenv('MODULE_VERSION')) 25 | } 26 | 27 | Sys.setenv(MODULESHOME=modulesHome) 28 | 29 | if( is.na(Sys.getenv('MODULEPATH', unset = NA)) ){ 30 | txt <- readLines(file.path(Sys.getenv('MODULESHOME'),"init/.modulespath")) 31 | 32 | # remove commented lines and trim leading and trailing whitespace 33 | txt <- gsub("^\\s+|\\s+$", "", sub("#.*$","",txt)) 34 | # paste together 35 | path <- paste(txt[txt != ""],collapse=":") 36 | Sys.setenv( MODULEPATH = path ) 37 | } 38 | 39 | if( is.na(Sys.getenv('LOADEDMODULES', unset = NA)) ){ 40 | Sys.setenv( LOADEDMODULES = "" ) 41 | } 42 | # for {r, engine="bash"} in Rmarkdown/other bash subprocesses to use 'module' function 43 | if( shell_has_bash() ) { 44 | module_function <- bash_func_name() 45 | if( is.na(Sys.getenv(module_function, unset = NA)) ) { 46 | env_var <- list(f = paste("() { eval `", file.path(modulesHome, "bin/modulecmd"), " bash ${1+\"$@\"}`; }", 47 | sep = "")) 48 | names(env_var) <- module_function 49 | do.call(Sys.setenv, env_var) 50 | } 51 | } 52 | } 53 | 54 | shell_has_bash <- function() { 55 | ## $SHELL is login shell bash - set by all shells 56 | ## $0 is process name 57 | ## $BASH is set by bash even when sh is symlinked to bash (/bin/sh rather than /bin/bash) 58 | bash <- 59 | system("{ which bash >/dev/null && bash -c 'basename ${SHELL:-unset}; basename $0; basename ${BASH:-unset}'; } || echo unset", 60 | intern = TRUE) 61 | any(bash == "bash") & any(bash == "unset") == FALSE 62 | } 63 | 64 | # probe the system to discover naming scheme BASH_FUNC_module%% vs BASH_FUNC_module() 65 | bash_func_name <- function() { 66 | ## system() uses sh which is often not linked/copy of bash, but another shell e.g. dash 67 | name_scheme <- system("bash -c '__r() { : ;}; export -f __r; env | grep ^BASH_FUNC___r'", 68 | intern = TRUE) 69 | name_scheme <- gsub(pattern = "(BASH_FUNC___r|=.*)", replacement = "", x = name_scheme) 70 | func_name <- paste("BASH_FUNC_module", name_scheme, sep = "") 71 | func_name 72 | } 73 | -------------------------------------------------------------------------------- /R/module.R: -------------------------------------------------------------------------------- 1 | #' Linux Environment Modules interface 2 | #' 3 | #' Access the linux Environment Modules which provides for the dynamic modification of a user's 4 | #' environment via modulefiles. 5 | #' 6 | #' Each modulefile contains the information needed to configure the shell for an application. 7 | #' Once the RLinuxModules package is initialized (with \code{\link{moduleInit}}), the environment can 8 | #' be modified on a per-module basis using the module command which interprets modulefiles. Typically 9 | #' modulefiles instruct the module command to alter or set shell environment variables such as PATH, 10 | #' MANPATH, etc. modulefiles may be shared by many users on a system and users may have their own 11 | #' collection to supplement or replace the shared modulefiles. 12 | #' 13 | #' @param Arguments "[ switches ] [ subcommand ] [ subcommand-args ]" See examples. 14 | #' @return Output messages will be sent to stderr 15 | #' 16 | #' @examples 17 | #' module("avail") # shows available modules 18 | #' 19 | #' module("--help") # show available sub-commands and switches 20 | #' 21 | #' module("load samtools") # loads the module "samtools" 22 | #' system("which samtools") # check that samtools is loaded in the environment 23 | #' 24 | #' module("list") # list loaded modules 25 | #' 26 | #' module("unload samtools") # unload the samtools module 27 | #' 28 | #' module("load samtools/1.0") # loads a specific version of the module "samtools" 29 | #' system("which samtools") # check that the correct samtools is loaded in the environment 30 | #' 31 | #' @export 32 | module <- function( Arguments ){ 33 | 34 | # check if arguments are corrext type 35 | if( !(class(Arguments) == "character" && length(Arguments)==1)){ 36 | stop("Arguments must be a character vector of length 1") 37 | } 38 | 39 | # check if module environment has been initialized 40 | if( is.na(Sys.getenv('MODULESHOME', unset = NA)) ){ 41 | stop("Environment variable MODULESHOME missing!\n", 42 | " Run moduleInit() to initialize module envionment" ) 43 | } 44 | 45 | moduleCmd <- file.path(Sys.getenv('MODULESHOME'),"bin/modulecmd") 46 | # check if modulecmd exists 47 | if(!file.exists( moduleCmd) ){ 48 | stop(moduleCmd," missing!\n", 49 | " Module environment not properly set up!" ) 50 | } 51 | 52 | # use the python interface 53 | pythonCmds <- system(paste(moduleCmd,"python",Arguments),intern=T) 54 | 55 | 56 | # Check if all python commands are recognizable 57 | validPythonCmd <- grepl("os\\.chdir\\('([^']*)'\\)",pythonCmds) | 58 | grepl("os\\.environ\\['([^']*)'] = '([^']*)'",pythonCmds) | 59 | grepl("del os\\.environ\\['([^']*)'\\]",pythonCmds) 60 | if( !all(validPythonCmd) ){ 61 | stop("modulecmd returned unknown command(s):\n", paste(pythonCmds[!validPythonCmd],collapse = "\n")) 62 | } 63 | 64 | # convert python commands to R commands 65 | RCmds <- sub("os\\.chdir\\('([^']*)'\\)","setwd(dir = '\\1')",pythonCmds,perl=T) 66 | RCmds <- sub("os\\.environ\\['([^']*)'] = '([^']*)'","Sys.setenv('\\1' = '\\2')",RCmds,perl=T) 67 | RCmds <- sub("del os\\.environ\\['([^']*)'\\]","Sys.unsetenv('\\1')",RCmds,perl=T) 68 | 69 | # execute R commands 70 | invisible( eval( parse(text = RCmds) ) ) 71 | } 72 | --------------------------------------------------------------------------------