Skip to content

Instantly share code, notes, and snippets.

View johnbaums's full-sized avatar

John johnbaums

  • University of Melbourne
  • Melbourne
View GitHub Profile
.bubbles {
stroke-width: 1px;
stroke: black;
opacity: .8
}
.bubbles:hover {
stroke: black;
}
* {
@johnbaums
johnbaums / sf_arrow.R
Last active November 7, 2022 03:03
Create arrows along great circles between pairs of points
sf_arrow <- function(from, to, len, deg, prefixFrom='from.', prefixTo='to.') {
# from, to: sf objects. `from` should be either a single point, or the same
# number of points as in `to`.
# len: the length of the arrow head, in map units.
# deg: the angle (in degrees) of the arrow head.
# prefixFrom, prefixTo: the attributes of `from` and `to` are added to the
# returned sf object. These prefixes will be prepended to the existing names
# of `from` and `to`.
crs <- st_crs(to)
n <- nrow(to)
@johnbaums
johnbaums / get_zotero.R
Created March 28, 2022 03:50
Download zotero collection, with pagination
library(dplyr)
library(jsonlite)
# create an empty list to store results
items <- list()
i <- 1 # initialise counter to 1. This is used to specify the start index for queries
# We can download at most 100 records at a time, so we download in pages,
# increasing the start index by 100 each time. We do this until the result of
# the query has fewer than 100 results (i.e. we've retrieved the last page of
@johnbaums
johnbaums / dms2dd.R
Created March 2, 2022 02:41
Convert coords from DMS to Decimal Degrees
dms2dd <- function(x) {
dms <- strsplit(x, '[^0-9.]+')
sapply(dms, function(x) {
sum(as.numeric(x)/c(1, 60, 3600)[seq_along(x)])
})
}
dms2dd(c('34°53’07”S', '138°36’40”E', '138.61111', '34°53’'))
## [1] 34.88528 138.61111 138.61111 34.88333
@johnbaums
johnbaums / process_hysplit_sims.R
Last active March 22, 2021 06:28
Parse HYSPLIT wind trajectory simulation results
process_hysplit_sims <- function(file) {
require(dplyr)
require(sf)
dat <- readLines(file)
starts <- grep('PRESSURE', dat) + 1
ends <- grep('New Simulation', dat) - 2
ends <- c(ends[ends > starts[1]], length(dat))
combined <- mapply(function(s, e, i) {
read.fwf(file=textConnection(dat[s:e]),
widths=c(6,6,6,6,6,6,6,6,8,9,9,9,9))
@johnbaums
johnbaums / collapse_numeric.R
Last active April 7, 2020 00:30
Collapse numeric in-text citations produced by bookdown, retaining links to chapter bibliography while ensuring all refs within collapsed ranges are included in the bibliography.
collapse_numeric <- function(index) {
# index: path to home page of bookdown-generated HTML book (e.g. index.html)
doc <- readLines(index)
pages <- file.path(
dirname(index), sub('.*data-path="(.*?)".*', '\\1',
grep('data-level="\\d+"', doc, val=T))
)
lapply(pages, function(p) {
x <- rmarkdown:::read_utf8(p)
library(jsonlite)
library(xml2)
library(rvest)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
d <- xml2::read_html('https://public.flourish.studio/visualisation/1619458/embed')
js <- rvest::xml_nodes(d, 'script') %>% html_text
@johnbaums
johnbaums / gs_coauthors.R
Created February 3, 2020 22:12
Get all listed coauthors for a given Google Scholar profile
gs_coauthors <- function(id) {
# id: a Google Scholar author ID.
# ^ e.g., for url https://scholar.google.com/citations?user=RWh6yJcAAAAJ&hl=en,
# the ID is 'RWh6yJcAAAAJ').
require(xml2)
require(rvest)
require(dplyr)
paste0('https://scholar.google.com.au/citations?view_op=list_colleagues&hl=en&json=&user=', id) %>%
xml2::read_html() %>%
rvest::html_nodes(., '.gs_ai_t') %>%
@johnbaums
johnbaums / doi2bib.R
Last active January 24, 2020 04:48
Return a bib file for a vector of DOIs
doi2bib <- function(dois, file=NULL, quiet=FALSE) {
# dois: vector of dois
require(httr)
bibs <- sapply(dois, function(x) {
if(!quiet) message(x)
response <- httr::GET(
'http://dx.doi.org', path=x,
add_headers(Accept = 'application/x-bibtex')
)
httr::content(response, as='text', encoding='UTF-8')
@johnbaums
johnbaums / fix_limits.R
Created January 22, 2020 22:50
Fix axis limits for overlapping lattice histograms
# See https://stackoverflow.com/q/59851541/489704
fix_limits <- function(p) {
if(!is.numeric(p$panel.args.common$breaks))
stop('trellis object should be constructed specifying nint')
b <- p$panel.args.common$breaks
pad <- diff(b[1:2])
lims <- lapply(p$panel.args, function(x) {
bins <- findInterval(x$x, b)
ymax <- max(unlist(tapply(bins, p$panel.args.common$groups[x$subscripts],
function(y) table(y)/length(y)))) + 0.025