Skip to content

Instantly share code, notes, and snippets.

View jhollist's full-sized avatar

Jeffrey W Hollister jhollist

View GitHub Profile
@jhollist
jhollist / iterate_waters.R
Created October 25, 2021 15:14
Example code for using a dataframe to access USEPA WATERS API. Shows two iteration examples: purrr and for loop.
library(purrr)
library(jsonlite)
library(dplyr)
library(usethis)
# The Data.gov API requires a key and has a 1000 request/hour limit
# Get Key from https://api.data.gov/signup/
# Save key in r environ
usethis::edit_r_environ()
# Add DATAGOV_KEY=YOURKEYGOESHERE to the file. Save and restart R
@jhollist
jhollist / word_count.R
Created October 9, 2014 20:10
An R function to count words of an input text file. Built and tested on an Rmd without any R code in it.
word_count<-function(txt_doc){
con<-file(txt_doc, "r", blocking=FALSE)
x<-readLines(con)
#Remove YAML front matter on Rmd
if(length(grep("---",x))>0){x<-x[-seq(1,max(grep("---",x)))]}
wrds<-0
for(line in x){
#Removes non character and splits
split_line<-strsplit(gsub("[^[:alnum:] ]", "", line), " +")[[1]]
#Removes empty string
library(USAboundaries)
library(sf)
library(dplyr)
library(elevatr)
library(raster)
# Get map of lower 48 states
usa_l48 <- us_boundaries() %>%
filter(state_abbr != "HI" &
state_abbr != "AK" &
@jhollist
jhollist / merge_linestrings.R
Created July 20, 2018 10:21
Combine multiple, sequential linestrings into one
library(sf)
#What I have
l1 <- st_linestring(matrix(c(1,1,2,2,3,1),ncol = 2, byrow = T))
l2 <- st_linestridng(matrix(c(3,1,3,2,4,2),ncol = 2, byrow = T))
l3 <- st_linestring(matrix(c(4,2,5,1,6,1),ncol = 2, byrow = T))
all_lines <- st_sfc(l1,l2,l3)
all_lines
#What I want
@jhollist
jhollist / bibify_pkgs.R
Last active March 14, 2018 21:49
An R function to turn a vector of package names into a bibliorgraphy
#' Generate a bibliography of packages
#'
#' This function creates an output bibliography from an input character vector
#' of package names. The primary use case for this function is when working
#' with other authors and you need to provide a reference list of packages used.
#'
#' @param pkgs A character vector of packages
#' @param csl A csl file used to format the output bibliography. Good place to
#' look for these is \url{https://github.com/citation-style-language/styles}
#' @param append_bib Existing \code{.bib} file to append package citations to.
@jhollist
jhollist / keybase.md
Last active September 18, 2017 10:48

Keybase proof

I hereby claim:

  • I am jhollist on github.
  • I am jhollist (https://keybase.io/jhollist) on keybase.
  • I have a public key whose fingerprint is D3F0 73AF 7C80 CAAA B965 4E0B 98E3 5E1B 139B 2FEF

To claim this, I am signing this object:

library(dplyr)
iris2 <- iris %>%
mutate(sp_abbr = case_when(Species == "virginica" ~ "vir",
Species == "setosa" ~ "set",
Species == "versicolor" ~ "vers"))
iris2
library(gh)
library(dplyr)
library(lubridate)
library(purrr)
usepa_members <- unlist(lapply(gh("/orgs/usepa/members", .limit = Inf),
function(x) x$login))
# Get all usepa repos, grab commits for each repo, filter for user and count
usepa_repos <- unlist(lapply(gh("/orgs/usepa/repos", type = "all", .limit = Inf),
function(x) x$name))
# Got too many private repos in a GitHub Organization?
# This will list all of the private repos you have access to and rank them by
# days since last push and total number of commits.
# Add PAT to .Renviron - mine is obscured
# cat("GITHUB_PAT=yourowngithubpatgoeshere\n",
# file=file.path(normalizePath("~/"), ".Renviron"),
# append=TRUE)
# http://api.census.gov/data/2010/sf1?get=P0010001,P0030001&for=block:1000&in=state:44+county:003+tract:020101
getCensus(name = "sf1",vintage = 2010, key = Sys.getenv("CENSUS_API_KEY"),
vars = "P0010001", region = "block:1000", regionin = "state:44+county:003+tract:020101")
# Throws error
# Error in data[, c(which(sapply(data, class) != "numeric"), which(sapply(data, :
# incorrect number of dimensions