Skip to content

Instantly share code, notes, and snippets.

@k-barton
k-barton / remove-fb-tracking.user.js
Last active September 27, 2023 16:58
Greasemonkey user script: Remove Facebook's external link tracking
// ==UserScript==
// @name Remove Facebook's external link tracking
// @description Removes redirection and the "click identifier" from external links on FBs
// @namespace https://gist.github.com/k-barton
// @match https://*.facebook.com/*
// @version 0.3.7
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
(function() {
@k-barton
k-barton / textbox.R
Last active June 27, 2023 01:04
Draw a rectangle surrounding text in a plot
# All arguments as in `graphics::text`, except pad which specifies padding (optionally
# for vertical and horizontal) around the text as a fraction of a character width.
# `...` is passed to `rect`
textbox <-
function(x, y, labels, adj = NULL,
pos = NULL,
offset = 0.5,
pad = c(0, 0),
vfont = NULL,
cex = par("cex"),
@k-barton
k-barton / pdfcombine.bat
Last active March 16, 2023 14:57
Windows batch script to combine mutiple pdfs into one using Ghostscript
@echo off
setlocal EnableDelayedExpansion
set result=
set outfile=
set nextisoutfile=
set "self=%~n0"
:startHelp
@k-barton
k-barton / MuMIn-methods-compar.gee.R
Last active December 8, 2022 15:47
Methods for `ape:compar.gee` needed to use these model types with MuMIn functions.
require("MuMIn")
local({
quasiLik.compar.gee <-
function (object, ...) {
scale <- object$scale
ret <- .qlik(object$residuals + object$fitted.values, object$fitted.values,
get(object$family, envir = asNamespace("stats"))(),
1, scale)
@k-barton
k-barton / MuMIn-methods-phylolm+pglmm_compare.R
Last active December 8, 2022 15:33
Standard methods for `phylo[g]lm` and `pglmm_compare` needed to use these model types with MuMIn functions.
local({
family.phylolm <-
function (object, ...) {
stop("???")
}
logLik.pglmm_compare <-
function (object, ...) {
rval <- object$logLik
@k-barton
k-barton / sd-ranef-avg.R
Last active May 13, 2022 12:37
Calculates model-averaged random effects std. dev.
sd.ranef.avg <-
function(object) {
if(!inherits(object, "averaging"))
stop("not an \"averaging\" object")
mo <- get.models(object, TRUE)
re <- lapply(mo, function(x) as.data.frame(ranef(x, condVar = TRUE)))
cols <- c("grpvar", "term", "grp")
@k-barton
k-barton / remove-google-tracking.user.js
Last active March 7, 2021 13:50
Greasemonkey user script: Remove Google's link tracking
// ==UserScript==
// @name Remove Google's link tracking
// @description Removes onmousedown event from external Google links to allow direct links
// @namespace https://gist.github.com/k-barton
// @include http://*.google.*
// @include https://*.google.*
// @version 0.2.1
// @grant unsafeWindow
// @run-at document-end
// ==/UserScript==
@k-barton
k-barton / mscrwpath.R
Created February 17, 2021 11:03
Generate a Multistate Correlated Random Walk path (preliminary version)
#' @return a two-column matrix of turning angles `"ta"` and step lengths `"len"`.
#' @param Nstates the number of movement states.
#' @param tp a vector of transition probabilities for `Nstates == 2` or a
#' square transition matrix `[Nstates, Nstates]`. Ignored if `Nstates == 1`
#' @param mu,rho wrapped Cauchy distribution parameters for the turning angles. Should have a length of `Nstates`.
#' @param wscale,wshape Weibull distribution parameters for step length
# TODO: starting position and initial direction. Currently all are set to 0.
mscrwpath <-
function(N, Nstates = 2, tp = c(.05, .1),
mu = c(0, 0), rho = c(.5, .99),
@k-barton
k-barton / subplot.R
Last active February 13, 2021 02:35
Create a subplot within the current plotting region (using base graphics).
#' @param expr an \R expression to produce the plot content.
#' @param fig numeric of length 1 or 2. The proportion of the plotting region to
#' be taken as the subplot plotting region (horizontal, vertical).
#' @param pos subplot position as a keyword, including "top", "bottom", "left",
#' "right". Empty string for centered plot.
#' @param inset numeric of length 1 or 2, giving inset distance(s) from the
#' margins in inches.
subplot <-
function(expr, fig = .33, pos = "topleft", inset = c(0, 0)) {
@k-barton
k-barton / pairs.cor.R
Last active February 8, 2021 13:55
Scatterplot matrix with linear model fit and correlation (base graphics)
#' @param formula,data,subset,na.action arguments to `model.frame`. If the first argument is a data frame, the rest is ignored.
#' @param method argument to `cor.test`. A character string indicating which correlation coefficient is to be used for the test. One of "pearson", "kendall", or "spearman", can be abbreviated.
#' @param n integer; the number of x values at which to evaluate.
#' @param lty,col,cex graphical parameters fit line type, line colour, character expansion for labels.
#' @param \dots optional, additional arguments passed to `points`
#' @example
#' data <- data.frame(x = runif(100), x1 = rlnorm(100), x2 = rnorm(100))
#' pairs.cor(~ x + log(x1) + x2, data)
pairs.cor <-
function(formula, data = NULL, subset = NULL, na.action = na.fail,