Skip to content

Instantly share code, notes, and snippets.

View MilesMcBain's full-sized avatar

Miles McBain MilesMcBain

  • Queensland Fire and Emergency Services
  • Brisbane, Queensland
View GitHub Profile
#devtools::install_github('rstudio/leaflet')
library(leaflet)
library(raster)
## fake a raster
r <- raster(volcano)
## this volcano is in NZ, but we are ignoring that with huge abandon
extent(r) <- extent(100, 150, -42, -15)
projection(r) <- "+init=epsg:4326"
@tslumley
tslumley / bentime.R
Created April 6, 2016 23:11
Times that don't turn into a pumpkin at midnight
"+.bentime"<-function(e1,e2){
e<-list(hour=e1$hour+e2$hour,min=e1$min+e2$min,sec=e1$sec+e2$sec)
sec_overflow<-e$sec>60L
e$sec<-e$sec %% 60L
e$min<-e$min+sec_overflow
min_overflow<-e$min>60L
e$hour<-e$hour+min_overflow
e$min<-e$min %% 60L
class(e)<-"bentime"
@fernandojsg
fernandojsg / gltf_bookmarklet.js
Last active September 1, 2017 13:54
THREE.js and AFRAME glTF exporter bookmarklet
javascript:(function(){
var link = document.createElement( 'a' ); link.style.display = 'none'; document.body.appendChild( link );
function save( blob, filename ) { link.href = URL.createObjectURL( blob ); link.download = filename; link.click(); }
function saveString( text, filename ) { save( new Blob( [ text ], { type: 'application/json' } ), filename );}
var script=document.createElement('script');
script.src='https://threejs.org/examples/js/exporters/GLTFExporter.js';
script.onload = function () {
var exporter = new THREE.GLTFExporter();
@wch
wch / vega-legend-update.html
Last active November 2, 2017 05:27
Example of vega data and legend updates
<!DOCTYPE html>
<html>
<head>
<script src="http://trifacta.github.io/vega/lib/d3.v3.min.js"></script>
<script src="http://trifacta.github.io/vega/vega.js"></script>
</head>
<body>
<script type="text/javascript">
var iris_spec = {
@jimjam-slam
jimjam-slam / rmd-snippets.json
Created February 22, 2018 23:15
Visual Studio Code snippets for working with RMarkdown files. Go to Configure User Snippets and drop it in.
{
// Snippets for talking to yourself
"TK (citation needed)": {
"prefix": "tkcite",
"body": "_(**TKTK** - citation needed)_",
"description": "Add a note to yourself to cite this later."
},
"TK": {
"prefix": "tktk",
"body": "_(**TKTK** - blah blah blah)_",
@whophil
whophil / ipython-notebook.conf
Last active August 18, 2018 05:15
Upstart file (Ubuntu) for Jupyter / IPython notebook
start on filesystem or runlevel [2345]
stop on shutdown
description "Jupyter / IPython Notebook Upstart script"
script
export HOME="/home/phil/Notebooks"; cd $HOME
echo $$ > /var/run/ipython_start.pid
exec su -s /bin/sh -c 'exec "$0" "$@"' phil -- /home/phil/Enthought/Canopy_64bit/User/bin/jupyter-notebook --config='/home/phil/.jupyter/jupyter_notebook_config.py'
end script
library(tidyverse)
library(sf)
library(mapview)
library(furrr)
library(raster)
plan(multiprocess)
ex = extent(c(xmin=-112,xmax=155,ymin=-44,ymax=-10))
# This example simulates a stock price 1 time and 10k times.
# It uses a functional approach and a loop approach.
# A speed test is done at the end.
# This example for GBM ignores the fact that
# 1) There is an explicit solution to GBM where simulation isn't needed
# 2) Along the same lines, there is a vectorized form where pure matrix mult can be used.
# These are ignored for the sake of the example, because there are often cases
# where this isn't the case and you HAVE to do the discrete loop.
@jennybc
jennybc / yaml_frontmatter_r_github_document.yaml
Last active February 9, 2022 21:36
YAML frontmatter for R Markdown to cause rmarkdown::render() to retain intermediate Markdown file for .R and .Rmd files, respectively
#' ---
#' title: "Something fascinating"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
#' ---
@jennybc
jennybc / 2014-10-12_stop-working-directory-insanity.md
Last active September 23, 2022 04:43
Stop the working directory insanity

There are packages for this now!

2017-08-03: Since I wrote this in 2014, the universe, specifically Kirill Müller (https://github.com/krlmlr), has provided better solutions to this problem. I now recommend that you use one of these two packages:

  • rprojroot: This is the main package with functions to help you express paths in a way that will "just work" when developing interactively in an RStudio Project and when you render your file.
  • here: A lightweight wrapper around rprojroot that anticipates the most likely scenario: you want to write paths relative to the top-level directory, defined as an RStudio project or Git repo. TRY THIS FIRST.

I love these packages so much I wrote an ode to here.

I use these packages now instead of what I describe below. I'll leave this gist up for historical interest. 😆