Skip to content

Instantly share code, notes, and snippets.

@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)
@wpetry
wpetry / rCompos.R
Last active April 9, 2018 07:21
Generate random compositions that sum to <=1
## Generate random compositions that sum to <=1
library(gtools)
library(ggtern)
library(scatterplot3d)
rCompos <- function(n, subsamples, alphas = NULL){
if(is.null(alphas)) alphas <- rep(1, subsamples)
sums <- matrix(runif(n), ncol = 1)
dirichlet <- gtools::rdirichlet(n, alphas)
# somewhat hackish solution to:
# https://twitter.com/EamonCaddigan/status/646759751242620928
# based mostly on copy/pasting from ggplot2 geom_violin source:
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
library(ggplot2)
library(dplyr)
"%||%" <- function(a, b) {