Skip to content

Instantly share code, notes, and snippets.

View btupper's full-sized avatar

Ben Tupper btupper

View GitHub Profile
@btupper
btupper / easy_example_stars.R
Last active August 5, 2025 19:00
Easy examples for sf and stars
suppressPackageStartupMessages({
library(sf)
library(stars)
library(dplyr)
})
nc = sf::read_sf(system.file("shape/nc.shp", package="sf"))
filename = system.file("nc/bcsd_obs_1999.nc", package = "stars")
x = read_ncdf(filename)
@btupper
btupper / extract_points.R
Created June 4, 2025 17:21
Extract point data from a stars object
#' Extract point data from a stars object
#'
#' @export
#' @param x stars object (with or without time dimension)
#' @param y sf point data
#' @param form chr one of either "long" (the default) or "wide" to control
#' output column layout.
#' @param ... other arguments passed to `stars::st_extract()`
#' @return table of variable data for each input point
extract_points = function(x, y,
@btupper
btupper / gist:7de35edb1ebebb3a852016acce6214c8
Last active June 11, 2024 20:01
Using googlesheets4 interactively
# https://drive.google.com/drive/folders/1vSdFzlbdT4khWeileTmN8gycNCINT2gS?usp=share_link
suppressPackageStartupMessages({
library(googlesheets4)
library(dplyr)
})
# authorize
gs4_auth()
@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