Skip to content

Instantly share code, notes, and snippets.

View btupper's full-sized avatar

Ben Tupper btupper

View GitHub Profile
#' A wrapper around base::findInterval() that allows decreasing values in the
#' value of the vector within which we wish to place values of x.
#'
#' When \code{vec} is in ascending order we use \code{base::findInterval()}, but
#' when \code{vec} is in descending order we implement an adaptation of the
#' \code{locate()} function from Numerical Recipes for C \url{http://apps.nrbook.com/c/index.html}
#'
#' @export
#' @param x numeric values we wish to located within \code{vec}
#' @param vec numeric vector of sorted values (ascending or descending order)
@btupper
btupper / oce_coastline.R
Created January 19, 2016 04:18
R utilties to convert oce::coastlineWorld and ocedata::coastlineWorld* coastlines to sp::SpatialLines
# coastline.R
bb <- c(-72,-63,39,46)
#' Convert a bbox to sp::polygon, sp::Polygons, or sp::SpatialPolygons object
#'
#' @export
#' @param bb numeric, a 4-element bbox vector [left, right, bottom, top]
#' @param projstring character sutiable to pass to \code{sp::CRS},
#' by default "+proj=longlat +datum=WGS84"
#' @param id character, the polygon ID, by default 'bbox'
@btupper
btupper / linux-nfs-volume-mounting
Created January 28, 2016 15:17
Mounting NFS volumes
##
# Q: I need to mount a new NFS volume "0.1.2.3:/nfs/some_volume" on my linux server as a directory, "/mnt/my_volume"
##
##
# A: Mounting a volume requires these steps (thanks, Joe!)
##
# 1. Log in to the server where you have sudo privileges.
@btupper
btupper / ed_extra.R
Created February 18, 2016 14:31
Extras for R rerddap package
#' Browse a dataset webpage.
#'
#' Note that it is an error to call this when \code{base::interactive()} returns FALSE
#'
#' @export
#'
#' @param datasetid Dataset id
#' @param x A datasetid or the output of \code{info}
#' @param url A URL for an ERDDAP server. Default: \url{http://upwell.pfeg.noaa.gov/erddap/}
#' @param ... Further args passed on to \code{\link[httr]{BROWSE}} (must be a named parameter)
@btupper
btupper / sample_layers.R
Last active March 27, 2016 15:27
R: Select N non-NA random points from a mulitlayer Raster* object
library(sp)
library(raster)
#' Extract valuers from a multilayer Raster*
#'
#' @param R a multilayer Raster* object
#' @param pts location info for points [cell, layer] or [row, col, layer]
#' @return a vector of values
layers_extractPoints <- function(R, pts){
@btupper
btupper / mgrepl.R
Last active August 20, 2020 13:59
grepl with multiple patterns
#' Perform grepl on multiple patterns; it's like AND-ing or OR-ing successive grepl statements.
#'
#' Adapted from https://stat.ethz.ch/pipermail/r-help/2012-June/316441.html
#'
#' @param pattern character vector of patterns
#' @param x the character vector to search
#' @param op logical vector operator back quoted, defaults to `|`
#' @param ... further arguments for \code{grepl} like \code{fixed} etc.
#' @return logical vector
mgrepl <- function(pattern, x, op = `|`, ... ){
@btupper
btupper / split_vector.R
Created November 16, 2016 13:24
Split a vector into a list of smaller vectors
#' Split a vector into groups of MAX (or possibly fewer)
#'
#' @param v vector or list to split
#' @param MAX numeric the maximum size per group
#' @return a list of the vector split into groups
split_vector <- function(v, MAX = 200){
nv <- length(v)
if (nv <= MAX) return(list('1' = v))
split(v, findInterval(1:nv, seq(from = 1, to = nv, by = MAX)))
}
@btupper
btupper / cs2cs.R
Created November 28, 2016 15:17
Access the cs2cs application from R
#' Access the cs2cs function from R
#'
#' @seealso http://proj4.org/index.html
#'
#' @param x numeric or character input value for x (longitude)
#' @param y numeric or character input value for y (latitude)
#' @param from character input coordinate system definition
#' @param to character output coordinate system definition
#' @param extra character extra arguments for cs2cs
#' @param app character the fully qualified path to the cs2cs executable
@btupper
btupper / du.R
Last active November 30, 2016 15:37
A function to audit disk usage by subdirectory
#' A function to audit disk usage by subdirectory
#'
#' @param path the path to audit
#' @param extra character extra arguments for linux `du` command
#' @param app character the `du` executable path
#' @return data frame or NULL
#' \itemize{
#' \item{size numeric in megabytes}
#' \item{name character}
#' }
@btupper
btupper / mset.R
Last active December 1, 2022 13:36
Set operations with multiple elements
#' Compute the union of a list of vectors
#'
#' @param x a list of zero or more vectors
#' @return a vector of the union or NULL is the input is empty
munion <- function(x){
if (!is.list(x)) stop("input must be a list")
n <- length(x)
if (n == 0) return(NULL)
if (n == 1) return(x[[1]])