Skip to content

Instantly share code, notes, and snippets.

View btupper's full-sized avatar

Ben Tupper btupper

View GitHub Profile
@btupper
btupper / purge_rstudio_configs.sh
Created December 26, 2023 14:46
Reset personal RStudio Server settings
#! /bin/sh
# purge_rstudio_configs
#
# Purpose:
# Refresh RStudio settings by moving per-user RStudio configurations into dated copies.
#
# Usage:
# $ purge_rstudio_configs
#
@btupper
btupper / st_miscellaneous.R
Last active December 1, 2022 15:52
Miscellaneous functions for simple features
#' Count intersections of y within geometries in x
#'
#' @param x sf object
#' @param y sf object
#' @param varname the name of the new count column
#' @return x with an added 'count' column
st_count <- function(x,
y,
varname = "count"){
@btupper
btupper / google_tokens.R
Created April 27, 2021 12:36
Google tokens
# Getting and saving a token for accessing documents in Google Drive
# this takes you through an interactive session tp exchange passcodes
token <-gargle::token_fetch(scopes = c("email", "https://www.googleapis.com/auth/drive"))
# then save it for use in another session
saveRDS(x, "~/.httr-token.rds")
token <- googlesheets::gs_auth(token = token)
# or for later session use
#' Retrieve a stars layer of Auckland’s Maunga Whau volcano topography. Adapted from
#' USGS-R inlmisc package.
#'
#' @seealso \url{https://waterdata.usgs.gov/blog/inlmiscmaps/}
#' @seealso \url{https://CRAN.R-project.org/package=inlmisc}
#' @export
#' @param indexed logical, if TRUE then assign 1,2,3,... as cell values
#' @return Stars
volcano_stars <- function(indexed = FALSE){
require(magrittr)
@btupper
btupper / nanosims.R
Created February 13, 2020 00:49
Read a NanoSIMS file (possibly corrupted header line)
# Read a NanoSIMS file (possibly corrupted header line)
# into a data frame (tibble). Possibly overwrite the orginal with the
# correct header line.
#
# Requires [readr](https://CRAN.R-project.org/package=readr) package
#
# Usage within R session:
# > source("/path/to/nanosims.R")
# # make resave FALSE to preserve the orginal file
# > x <- read_nanosims("/path/to/nanosims/file.csv", resave = TRUE)
@btupper
btupper / document_table.R
Created August 28, 2019 13:48
Simplified roxygen style documentation for data frames
#' Build out an empty itemization table for a data frame
#'
#' @param x data.frame or tibble
#' @return character string, but it is printed, too
itemize_table <- function(x){
klass <- sapply(x, function(x) class(x)[[1]])
varnames <- names(klass)
s <- c("\\itemize{",
sprintf(" \\item{%s %s }", varnames, klass),
"}")
@btupper
btupper / record-lab-intro.md
Created July 12, 2018 16:00
record-lab-intro

R packages

All components of R come in packages (compressed files with code and data inside) that are stored in one of two places on a computer: (1) a user's personal library OR (2) in a system-wide library available to all users. You'll be placing packages in your personal library as you don't have administrative credentials on the Mac Mini.

When R is installed it comes with 'base' packages for working with files, simple graphing, statistics, and working with the operating system (likely macOS in your case). When you start a new session (aka "instance") these 'base' packages are loaded

@btupper
btupper / split_matrix.R
Last active December 17, 2020 15:35
Split matrices by row
#' Split a matrix by row much as is done for \code{data.frame}
#'
#' @param x the matrix
#' @param f a ‘factor’ see \code{split}
#' @param ... other arguments passed to \code{split}
#' @return a list of matrices
split_matrix <- function(x, f, ...){
rnm <- rownames(x)
if (is.null(rnm)){
@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]])
@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}
#' }