Skip to content

Instantly share code, notes, and snippets.

View jefferys's full-sized avatar

Stuart R. Jefferys jefferys

  • UNC Chapel Hill
View GitHub Profile
@jefferys
jefferys / micromambaSetup.sbatch
Created January 8, 2024 19:52
Micromamba local self-contained environment setup using Slurm if avaialble
#!/usr/bin/env bash
#SBATCH --job-name mambaEnv
#SBATCH --cpus-per-task 6
#SBATCH --mem 16G
#SBATCH --partition allnodes
#SBATCH --output ./mambaEnv.%j.out
#SBATCH --error ./mambaEnv.%j.err
# yaml filename is required argument 1
@jefferys
jefferys / ensurePackages.R
Last active February 18, 2021 23:46
Check for needed R packages, optionally installing them.
hasPackage <- function( package ) {
suppressWarnings( suppressMessages(
require( package, quietly= TRUE, warn.conflicts= FALSE, character.only= TRUE )
))
}
# Set progress=FALSE to hide progress messages. Install messages always
# displayed as they are changing the system.
ensurePackages <- function( packages, installOk= FALSE, progress= TRUE,
exitOnError= installOk ) {
@jefferys
jefferys / mydir.bash
Created December 28, 2020 20:46
Get the directory of a path. Example use is to get a bash script's location for loading other bash scripts as modules.
#!/usr/bin/env bash
# Prints error messages to stderr
# @PARAM Concatenates parameters with IFS (default space) to form an error
# message.
# @OUT Prints error message preceeded by "ERROR: " to standard error,
# translating escapes.
# @REQUIRES: echo -e
function sayErr() {
echo -e "ERROR: $@" >&2
@jefferys
jefferys / logSay.sh
Created December 17, 2020 19:46
Bash program messages - terminal and log file printing
# USAGE:
# say $LEVEL_INFO "This is an" "informational message."
# #> "This is an informational message."
#
# sayError "This is an" "error message."
# #> "ERROR: This is an error message."
#
# LEVELS:
# In order from highest priority message to lowest:
# $LEVEL_ALL $LEVEL_ERROR $LEVEL_WARN $LEVEL_INFO $LEVEL_VERBOSE
@jefferys
jefferys / parseCli.sh
Created December 17, 2020 19:39
Bash option parsing example without using getopt and using globals instead of a hash
# Avoids string-keyed arrays as not available in old bash on Macs. Supports:
# * arguments and values with spaces if quoted.
# * arguments embedded in options (e.g. "commands").
# * arguments that look like option (start with a -) after a lone "--"
# * options with values, without values, and with optional values.
# * repeated options with values and counted flags.
# * both "key=value"" and "key value" style options
function parseCli() {
# globals
OPT_DOWNLOAD=0
# Append potentially empty string, but with separator if not empty
# "one" -> "one" if "$extra" is empty, "one" -> "one, two" if not
result="one${extra:+", ${extra}"}"
# Examples
extra=;[ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra=''; [ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra='two'; [ "one${extra:+", ${extra}"}" == 'one, two' ] || echo "FALSE"
# Useful for path changes adding possibly empty dir to path
@jefferys
jefferys / bash_get_options_template.sh
Created November 14, 2017 21:43
A template for a bash function parsing CLI options and arguments into global variables, including short, long, repeated, counted, and --.
###############################################################################
# DESC: Parses arguments with the bash built-in getopts, but allows for
# long options too. This only works when each long option has a short option,
# although short options need not have long ones. Designed to create global
# variables named opt_<option> containing the parsed command line options, and
# an array variable called opt_args with any additional non-option arguments.
# This is all hard coded, but simple to modify for local use. See the ###
# sections for what needs to be changed to create your own option variable set.
# Supports bundled options and the use of "--" to signal end of options.
# Does not support negated options. You can always just declare "myFlag" and
@jefferys
jefferys / roxygen2Markup.gist.R
Created June 21, 2015 17:34
R text markup with roxygen2 - style, lists, links, tables and equations
#==========================
# Demo roxygen2 text markup
#==========================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#' Demo page for roxygen2 text markup
#'
@jefferys
jefferys / roxygen2MultiFunctionDocumentation.gist.R
Created June 21, 2015 17:29
R function documentation with roxygen2 - multiple functions
#===================================================
# Demo multi-function roxygen2 - three ways to do it
#===================================================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#====================================================
# Demo multi-function roxygen2 page using @describeIn
@jefferys
jefferys / roxygen2PackageDocumentation.gist.R
Last active September 3, 2022 22:13
R package documentation with roxygen2
#====================================
# Demo package roxygen2 documentation
#====================================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#' Brief page title describing this package.
#'