Skip to content

Instantly share code, notes, and snippets.

@wpetry
wpetry / export_drive.R
Created March 31, 2025 16:24
Export a Google Drive folder, converting files to locally-editable file types
export_drive <- function(url, dir = ".", quiet = TRUE, progress = TRUE) {
require(googledrive)
require(dplyr)
require(purrr)
if (!dir.exists(dir)) {
message("Directory ", dir, " does not exist.")
md <- menu(c("Yes", "No (exit)"), title = "Do you want to create it?")
if (md == 1L) {
dir.create(dir, recursive = TRUE)
@wpetry
wpetry / v_distance_along.R
Created March 26, 2025 17:13
Calculates the distance of points along a line with simple features in R, mirroring GRASS GIS v.distance with *_along
v_distance_along <- function(points, line, dist_unit = "km") {
# packages
require(sf)
require(sfnetworks)
require(dplyr)
require(units)
# check inputs
if (!inherits(points, "sf") && !inherits(points, "sfc")) {
stop("'points' must be an sf or sfc object containing POINT or MULTIPOINT geometries.")
}
@wpetry
wpetry / get_bb_snow.R
Created January 19, 2023 13:19
scrape billy barr's long-term snow data from https://www.gothicwx.org/
get_bb_snow <- function(){
require(rvest)
require(tidyverse)
require(janitor)
require(lubridate)
snow <- rvest::read_html("https://www.gothicwx.org/long-term-snow.html") %>%
rvest::html_element(".tableizer-table") %>%
rvest::html_table(header = TRUE, na.strings = c("NA", "")) %>%
janitor::row_to_names(1) %>%
@wpetry
wpetry / unimodal_GAMM.R
Created October 6, 2022 21:13
fit GAMM to test for unimodality
library(tidyverse)
library(lme4)
library(gamm4)
# simulate data
set.seed(238472)
simdat <- tibble(env = c(seq(0, 1, length.out = 20),
seq(2, 3, length.out = 20),
seq(4, 5, length.out = 20)),
site = rep(letters[1:3], each = 20),
@wpetry
wpetry / scrape_WoS.R
Created October 6, 2021 17:56
Download search results from a Web of Science web query
#################################################-
## Download search results from a Web of Science web query ----
## W.K. Petry
##
## example usage:
## get_wos_query(url = "https://www.webofscience.com/wos/woscc/summary/83c53a2b-5a39-4468-84f0-c9ffcfba5e91-01b947ba/relevance/1", profile = fprof)
#################################################-
## Define function to fetch WoS query hits ----
#################################################-
get_wos_query <- function(url, browser = c("firefox", "chrome", "phantomjs"),
@wpetry
wpetry / git_status.R
Last active July 25, 2019 19:42
Checks status of all git repos in a directory
#################################################-
## Functions to check status of git repos in a given folder ----
## W.K. Petry
##
## inspired by @djnavarro's workbch::view_git_status
#################################################-
## Helper fxn to fail gracefully for local-only repos ----
## with no upstream
#################################################-
ab_poss <- purrr::possibly(.f = ~as.data.frame(as.list(git2r::ahead_behind(local = .x, upstream = .y)),
@wpetry
wpetry / sf_cutregions.R
Last active August 20, 2018 11:14
Use sf to cut multipolygons by regional multipolygon
#################################################-
## Subdivide multipolygon by regional multipolygon with sf ----
## W.K. Petry
#################################################-
## Preliminaries ----
#################################################-
library(sf)
library(tidyverse)
library(cowplot)
@wpetry
wpetry / plot.windrose.R
Created July 28, 2018 12:25
plot histogram of wind speed and direction on polar coordinates
# WindRose.R
# modified from https://stackoverflow.com/questions/17266780/wind-rose-with-ggplot-r
require(ggplot2)
require(RColorBrewer)
require(dplyr)
require(tidyr)
plot.windrose <- function(data,
spd,
dir,
@wpetry
wpetry / get_pkg_versions.R
Last active July 5, 2018 11:42
function that reports the names and version numbers of packages in an R workspace
#' Retrieve attached or loaded package names and their version numbers
#'
#' @param L an object of class 'sessionInfo'. Default is to retrieve the current
#' workspace session information.
#' @param n character specifying whether to use attached packages ('otherPkgs') or
#' packages only loaded via a namespace ('loadedOnly'). Base packages are always
#' omitted.
#'
#' @return a data frame with two columns: the package name and the package version number.
#' @export
#################################################-
## Evaluate color palettes for colorblindness accessibility ----
## W.K. Petry
#################################################-
## Preliminaries ----
#################################################-
library(colorspace)
library(colorscience)
library(paletteer) # devtools::install_github("EmilHvitfeldt/paletteer")
library(tidyverse)