Skip to content

Instantly share code, notes, and snippets.

@kaneplusplus
kaneplusplus / save-surv-plot.r
Created April 6, 2022 21:31
Write a ggsurv object like you would a ggplot object.
library(survival)
library(survminer)
library(patchwork)
library(ggplot2)
ggsurvsave <- function(filename, plot = last_plot(), ...) {
if (!inherits(plot, "ggsurvplot")) {
stop("Argument plot does is not a ggsurvplot object.")
}
@kaneplusplus
kaneplusplus / tmp-tbl.r
Last active June 13, 2022 14:25
`tmp_tbl()` create a database-backed tbl that gets dropped when the handle goes out of scope
library(dplyr)
library(dbplyr)
library(uuid)
tmp_tbl <- function(x, con, tbl_name = UUIDgenerate()) {
uuid <- UUIDgenerate()
db_copy_to(con, tbl_name, as.data.frame(x), temporary = TRUE)
ret <- tbl(con, tbl_name)
ret$info <- new.env()
ret$info$con <- con
@kaneplusplus
kaneplusplus / spatial_in.r
Last active January 22, 2018 04:47
Find points that are %in% a SpatialPolygonsDataFrame
library(sp)
library(raster)
setClass("coords", contains="matrix")
# x should be a matrix. The first column holds longitudes, the
# second holds latitudes.
coords <- function(x) {
if (!inherits(x, "matrix") && !inherits(x, "data.frame")) {
stop(paste0("Don't know how to make coordinates from an object ",

Keybase proof

I hereby claim:

  • I am kaneplusplus on github.
  • I am kaneplusplus (https://keybase.io/kaneplusplus) on keybase.
  • I have a public key ASDluGe-p-mg79KbiEspTaL5ttVxqqt87OqICU0rRooebQo

To claim this, I am signing this object:

@kaneplusplus
kaneplusplus / gist:0153d8d91412ce0ddcdb772b0fec4f35
Created April 22, 2016 15:18
Code from my NESS 2016 Short Course
###########################################
# Install required packages.
###########################################
install.packages(c("iotools", "rbokeh",
"tidyr", "foreach", "doParallel",
"datadr", "trelliscope"))
# Download the Airline OnTime dataset.
url_prefix="http://stat-computing.org/dataexpo/2009/"
@kaneplusplus
kaneplusplus / glmnet-ref.r
Last active January 17, 2016 20:40
A minimal glmnet implementation in R
soft_thresh = function(x, g) {
x = as.vector(x)
w1 = which(g >= abs(x))
w2 = which(g < abs(x) & x > 0)
w3 = which(g < abs(x) & x < 0)
ret = x
ret[w1] = 0
ret[w2] = x[w2]-g
ret[w3] = x[w3]+g
ret
@kaneplusplus
kaneplusplus / gist:1fe92c061b32f2f7e6f1
Created December 17, 2014 20:24
Find the synonyms of a word in R
library(wordnet)
library(foreach)
get_synonyms = function(x) {
unique(foreach(pos=c("ADJECTIVE","ADVERB","NOUN","VERB"), .combine=c) %do% {
filter <- getTermFilter("ExactMatchFilter", x, TRUE)
ret = NULL
try({
suppressWarnings({
terms <- getIndexTerms(pos, Inf, filter)
@kaneplusplus
kaneplusplus / gist:9943047
Created April 2, 2014 20:58
Memoize the result of an expression in R
require(rredis)
redisConnect()
memoize <- function(expr, key=NULL, expire_time=Inf, verbose=FALSE,
envir=parent.frame()) {
if (is.null(key)) {
key <- paste(substitute(expr), collapse="")
}
if (redisExists(key)) {
@kaneplusplus
kaneplusplus / gist:6067445
Last active December 20, 2015 03:59
A parallel, block-matrix multiply for dense and sparse R matrices.
require(foreach)
require(itertools)
# block matrix multiplication
bmm <- function(x, y, chunkSize=max(1, floor(nrow(x)/6)), verbose=FALSE,
writeBlock=FALSE, returnResult=TRUE, filePrefix="block.", writeDir="",
projectionBlocks=FALSE) {
if (ncol(x) != nrow(y))
stop("Non-conformable matrices")
if (verbose) {