Skip to content

Instantly share code, notes, and snippets.

View Sleepingwell's full-sized avatar

Simon Knapp Sleepingwell

  • Canberra, ACT, Australia
View GitHub Profile
@Sleepingwell
Sleepingwell / deep_thought.R
Last active August 29, 2015 13:56
An example how to make R code hard to find using C.
setwd("c:/dir/containing/compiled/binary")
dyn.load("deep_thought")
.External('the_meaning_of_life', environment())
.External('the_meaning_of_multiple_lives', 42, environment())
dyn.unload("deep_thought")
@Sleepingwell
Sleepingwell / gist:8908692
Created February 10, 2014 01:19
Some comparative timing for various iterators and loops.
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/any_range.hpp>
#include <boost/range/numeric.hpp>
#include <boost/timer/timer.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <numeric>
@Sleepingwell
Sleepingwell / gist:8896424
Created February 9, 2014 09:05
Some thoughts on a spatial temporal container.
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <boost/ref.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
using namespace std;
@Sleepingwell
Sleepingwell / gist:7701655
Created November 29, 2013 04:41
Code used to compare looping performance of R/C++ and Julia.
/**
* This code is subject to the unlicense (http://unlicense.org/).
*
* I used it to compare the performance of C++ code called from R against native Julia.
* It is based on the blog at:
*
* http://feedly.com/e/B5JIqzW8?goback=%2Egde_77616_member_5811511767066509316#%21
* (accessed on 29 Nov, 2013)
*
* The Julia equivalent is given below (just incase that link disappears).
@Sleepingwell
Sleepingwell / gist:7445312
Last active February 11, 2021 04:23
Example of using geoalchemy.
# A simple script to load data from a shapefile into a spatialite db.
# Transfering the geometries is slow. It would probably be much faster to
# use WKB directly for the load, but I cannot get this to work.
# This is largely copied from
# https://github.com/geoalchemy/geoalchemy/blob/master/examples/spatialite.py
import os, osgeo.ogr
from sqlite3 import dbapi2 as sqlite
from sqlalchemy.orm import sessionmaker
from sqlalchemy import event, create_engine, MetaData
@Sleepingwell
Sleepingwell / gist:6765037
Last active December 24, 2015 07:39
XML to data frame.
library(XML)
MYDATA <- readLines('candidate1.xml')
MYDATA <- sub("UTF-16", "UTF-8", MYDATA)
MYDATA <- xmlInternalTreeParse(MYDATA, asText = TRUE)
MYDATAlist <- xmlToList(MYDATA, addAttributes = TRUE, simplify = FALSE)
tmp <- MYDATAlist[names(MYDATAlist) == 'Item'] #MYDATAlist[names(MYDATAlist) != '.attrs']
tmp <- lapply(tmp, function(x) tryCatch(
cbind(Item_ID=x$.attrs['Item_ID'], do.call(rbind, x[names(x) == "Answer_Item"]))[,1:3],
error=function(e) NULL
))
@Sleepingwell
Sleepingwell / gist:6726433
Created September 27, 2013 10:03
How to merge a list of data frames in R
# dummy data
maxN <- 10
listLen <- 6
Ns <- sample(as.integer(maxN/2):maxN, listLen)
dat <- lapply(Ns, function(N) data.frame(country=sample(maxN, N), var=rnorm(N)))
# function that (recursively) does the merging
merger <- function(lst, var.name) if(length(lst) == 1) lst else merge(lst[[1]], merger(lst[-1], var.name), by=var.name, all=T)
# do the merge and put sensible names on the result, and fix the NAs (set to zero)
@Sleepingwell
Sleepingwell / gist:6604496
Created September 18, 2013 04:18
A little mechanism for setting options for an R package.
opt.env <- new.env()
setOption <- function(auth_token, remaining_limit) {
cl <- as.list(match.call()[-1])
invisible(mapply(assign, names(cl), cl, MoreArgs=list(envir=opt.env)))
}
getOption <- function(opt) if(exists(opt, envir=opt.env)) get(opt, envir=opt.env) else NA
#---- test ---
@Sleepingwell
Sleepingwell / gist:6418936
Last active December 22, 2015 04:39
An example of using showdown to parse markdown in a web page + syntax highlighting.
<html>
<!-- based example found at: http://blog.harakys.com/blog/2012/02/21/embed-markdown-into-your-html/ -->
<header>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/prettify.js"></script>
<script src="http://www.showdown.im/showdown/example/showdown.js"> </script>
<script type="text/javascript">
var generateHTML = function() {
@Sleepingwell
Sleepingwell / move_or_copy.py
Last active January 29, 2024 12:36
An example of how to copy or move a bunch of files matching a specific regex with python.
# Copyright (C) 2013 Simon Knapp
#
# This program is free software; you can redistribute it and/or
# modify it under the any terms you wish.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# To run this, type: