Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@emhart
emhart / refexp.r
Created November 16, 2012 21:23
Blog post on random effects in mixed models
library(lme4)
library(ggplot2)
#create some levels
levs <- as.factor(c("l1","l2","l3","l4","l5"))
#set the factor means
f_means <- c(6,16,2,10,13)
# set individual as a factor
@emhart
emhart / server.R
Created December 10, 2012 21:22
rOpenSci rgbif shiny app test
library(shiny)
library(ggplot2)
library(rgbif)
## Set up server output
shinyServer(function(input, output) {
# define function for server plot output
output$gbifplot <- reactivePlot(function() {
splist <- input$spec
'''
import csv
open("test.csv","w").close()
dat = ['one potato','two potato']
dat2 = ['3 potato','four']
dat3 = ['8 potatos','9 potatoes']
mydat = [dat,dat2,dat3]
with open("test.csv","a") as csvfile:
mywriter = csv.writer(csvfile, delimiter=",")
for x in mydat:
@emhart
emhart / theme_blank.R
Last active December 22, 2017 07:56
A blank ggplot2 theme
theme.blank = function(size=12) {
theme(
#axis.line=element_blank(),
axis.text.x=element_text(size=size),
axis.text.y=element_text(size=size),
#axis.ticks.y=element_text(size=size),
# axis.ticks=element_blank(),
axis.ticks.length=unit(0.1, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_text(size=size*1),
@emhart
emhart / loops_vs_apply.R
Last active December 14, 2015 20:59
timing in loops vs apply functions...
library(ggplot2)
# n is the number of iterations for the test
# j is the number of matrices to create
# m is the size of the square matrix to solve
loops <- function(n,j,m){
for(i in 1:n){
mylist <- list()
for(x in 1:j){mylist[[x]]<- matrix(rnorm(m^2),nrow=m,ncol=m)}
for(x in 1:j) {z <- solve(mylist[[x]])}
@emhart
emhart / ecoODE.R
Created March 28, 2013 20:49
Examples of ODE's in R from classic ecological models with a simple M-W extension on I
#' Description: Code using deSolve for some simple ecological ODE models
#' There are 3 examples, one is a simple LV Pred-Prey example with plots
#' The next is a Macarthur-Wilson model with plots of both the species curve and the equlibrium points
#' The last plot is a quick crack at putting stochasticity into the model by adding oscillations in I with t.
#' LV code is modified from a blog post here: http://assemblingnetwork.wordpress.com/2013/01/31/two-species-predator-prey-systems-with-r/
#' Other additions are my own
#'
#' Author: Edmund Hart
#' Date: 3/28/2013
#' E-mail: edmund.m.hart@gmail.com
@emhart
emhart / draw_srpk_tri.py
Last active December 17, 2015 06:19
This is a function I worked on with a student at the Vancouver Ladies Learning Code workshop in Vancouver on May 11th, 2013. I added the docstring, and cleaned up a few things, but most of the work was done by June Yakavonis.
import random as rn
from ipythonblocks import BlockGrid
def draw_srpk_tri(dim = 100, iterations = 1000, background_color = (255,255,255), triangle_color = (0,0,0) ,block_size = 3):
'''
Draws a Sierpinski triangle using the chaos game approach (http://en.wikipedia.org/wiki/Chaos_game)
Parameters:
dim -- integer, the dimensions of the square to draw the triangle in, default is 100
iterations -- integer, the number of points to create the triangle with, the higher the value, the more resolution, default is 1000
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@emhart
emhart / xtable_raw_text.R
Created June 12, 2013 04:32
For Scott so he can make html tables.
require(xtable)
insane <- data.frame(Name=c("Ampersand","Greater than","Less than","Underscore","Per cent","Dollar","Backslash","Hash", "Caret", "Tilde","Left brace","Right brace"), Character = I(c("&",">", "<", "_", "%", "$", "\\", "#", "^", "~","{","}")))
colnames(insane)[2] <- paste(insane[,2],collapse="")
print( xtable(insane),type='html',sanitize.text.function = function(x){x})
@emhart
emhart / gbif_climatemap.R
Created July 23, 2013 20:46
gbif data and climate maps.
library(rWBclimate)
library(rgbif)
splist <- c("Junco hyemalis", "Aix sponsa")
out <- occurrencelist_many(splist, coordinatestatus = TRUE, maxresults = 20)
options(kmlpath = "/Users/edmundhart/kmltemp")
sp.map.df <- create_map_df(c("USA","MEX"))
sp.map <- ggplot(sp.map.df,aes(x=long,y=lat,group=group))+geom_polygon(fill="white",colour="black")+xlim(-130,-65)+ylim(12,50)
sp.map + geom_point(data=gbifdata(out),aes(y=decimalLatitude,x=decimalLongitude,group=taxonName,colour=taxonName))