Skip to content

Instantly share code, notes, and snippets.

@MrFlick
MrFlick / between.R
Last active August 29, 2015 14:01
between.R: checks if a number is between two other numbers (inclusive)
between <- function(x, a, b) {
if(missing(b)) {
if(length(a)==2) {
a<-t(a)
}
} else {
a <- unname(cbind(a,b))
}
a<-t(apply(a,1,sort, na.last=T))
a[,1] <= x & x <= a[,2]
@MrFlick
MrFlick / classMethods.R
Last active December 12, 2015 11:10
classMethods.R: list all generic functions for a given class
classMethods <- function(cl) {
if(!is.character(cl)) {
cl<-class(cl)
}
ml<-lapply(cl, function(x) {
sname <- gsub("([.[])", "\\\\\\1", paste0(".", x, "$"))
m <- methods(class=x)
if(length(m)) {
data.frame(
m=as.vector(m),
@MrFlick
MrFlick / test.withX.R
Last active August 29, 2015 14:01
withX.R: Allows you to perform multiple operations on a temporary object without having to create a temporary variable
# withX() will create an environment for the first parameter and all of your named parameters
# if the first parameter is not named, it will be called "X" in the enviroment
# there may be exactly one unnamed expression
# this unnamed parameter will be eval'ed in the envir defined by your named parameters
# If no object is returned from the expression, the newest value of the first parameter is returned invisibly
# this can avoid repeating awkward expressions
#this is a lot like `with()` but instead of working within the variable,
# the value itself is added to the enviroment as a variable
@MrFlick
MrFlick / makeglm.R
Last active October 7, 2023 11:28
makeglm.R: Creates a "fake" glm object with specific coefficients that you can use for predicting without fitting a model first
makeglm <- function(formula, ..., family, data=NULL) {
dots <- list(...)
out<-list()
tt <- terms(formula, data=data)
if(!is.null(data)) {
mf <- model.frame(tt, data)
vn <- sapply(attr(tt, "variables")[-1], deparse)
if((yvar <- attr(tt, "response"))>0)
vn <- vn[-yvar]
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
@MrFlick
MrFlick / capture.R
Created October 3, 2014 02:48
capture.R: make sure variable is assigned in local enviroment
capture <- function(...) {
vars<-sapply(substitute(...()), deparse)
pf <- parent.frame()
Map(assign, vars, mget(Filter(exists, vars), envir=pf, inherits = TRUE), MoreArgs=list(envir=pf))
}
@MrFlick
MrFlick / elements.txt
Last active August 29, 2015 14:17
Periodic Table
# from the sample data included with http://www.ucancode.net/Visual_C_Control/Draw-table-shape-vc-example-periodic.htm
"AtomicNumber","AtomicWeight","GroupNumber","GroupName","Period","Block","CASRegistryID","ElementName","ElementSymbol","DiscoveryDate","DiscovererName","Row","Column"
1,1.00,5,"Non-metals","1","s-block","1333-74-0","Hydrogen","H","1766","Henry Cavendish",1,1
2,0.00,4,"Noble gases",,,,"Helium","He",,,1,18
3,0.00,0,"Alkali earth",,,,"Lithium","Li",,,2,1
4,0.00,1,"Alkaline earth",,,,"Beryllium","Be",,,2,2
5,0.00,3,"Metalloids",,,,"Boron","B",,,2,13
6,0.00,5,"Non-metals",,,,"Carbon","C",,,2,14
7,0.00,5,"Non-metals",,,,"Nitrogen","N",,,2,15
8,0.00,5,"Non-metals",,,,"Oxygen","O",,,2,16
@MrFlick
MrFlick / export.R
Created October 29, 2015 00:53
export.R: copy variables from current environemnt to the calling environment
export <- function(...) {
dots <- substitute(...())
dp <- sapply(dots, deparse)
names <- if (is.null(names(dots))) rep("", length(dots)) else names(dots)
names[names==""] <- dp[names==""]
for(i in seq_along(dots)) {
assign(names[i], eval(dots[[i]], parent.frame()), envir=parent.frame(2))
}
}
to.minutes <-function(x) {
#regexpr("(\\d{1,2}:)?\\d{1,2}\\.\\d{1,2}", x, perl=T)
idx<-gregexpr(":", x, perl=T)
# try to turn hh:mm:ss interval to a count in minutes.
mapply(function(ss,ids) {
if(length(ids)==2) {
as.numeric(substr(ss,1,ids[1]-1)) * 60 +
as.numeric(substr(ss,ids[1]+1, ids[2]-2))+ as.numeric(substr(ss,ids[2]+1, nchar(ss)))/60
} else if (length(ids)==1) {
as.numeric(substr(ss,1,ids[1]-1)) + as.numeric(substr(ss,ids[1]+1, nchar(ss)))/60
@MrFlick
MrFlick / arrow-navigate.js
Created May 23, 2016 21:37
Trigger Next/Prev links with arrow keys
// Here You can type your custom JavaScript...
//for use with "Custom Javascript for Websites" chrome extension
//adds arrow key navigation for next/prev links
$(document).keyup(function(event) {
if (event.which==39) {
//right
var nextLink = $('a').filter(function(index) { return $(this).text() === "next"; });
window.location.href = nextLink.attr("href")