Skip to content

Instantly share code, notes, and snippets.

View johnbaums's full-sized avatar

John johnbaums

  • University of Melbourne
  • Melbourne
View GitHub Profile
@johnbaums
johnbaums / get_namespaces.R
Last active February 8, 2022 14:02
Get the unique set of namespaces specified in an R script
get_namespaces <- function(file, as_imports=FALSE) {
require(stringi)
require(dplyr)
nm <- readLines(file) %>%
stringi::stri_extract_all(regex='[\\w_.]+::[\\w_.]+') %>%
unlist %>%
setdiff(NA) %>%
sort
@alexander-matz
alexander-matz / graphiq.Rscript
Created October 11, 2018 14:27
Nice Graph Colors from the Graphiq Blog
# Color palettes that actually look good from 1 .. 12 colors
# Taken from: https://blog.graphiq.com/finding-the-right-color-palettes-for-data-visualizations-fcd4e707a283
graphiq <- function (n, option = 'warm') {
if (n < 1 || n > 12) stop('colors only defined for n = 1..12');
if (option == 'warm') {
return(switch(n,
c("#FDB25F"),
c("#FFC96B", "#F47942"),
c("#FFC96B", "#F47942", "#AB412C"),
c("#FFD773", "#F99851", "#EF5833", "#923E2D"),
@oscarperpinan
oscarperpinan / inset.R
Created August 30, 2016 07:24
Inset graphics with rasterVis and grid
library(raster)
library(rasterVis)
library(grid)
r <- raster(system.file("external/test.grd", package="raster"))
## Main graphic
p1 <- levelplot(r)
## Inset graphic
p2 <- levelplot(r,
margin = FALSE,
@brodieG
brodieG / sample0110b.R
Last active April 14, 2017 15:04
blind sample implementation variation
# http://stackoverflow.com/a/30781090/2725969
sample0110b <- function(size, n) {
size <- as.integer(size)
n <- as.integer(n)
if(size > 25 || size < 3L) stop("Size out of valid range")
# Generate integer pool and weights
@brodieG
brodieG / sample0110.R
Last active August 29, 2015 14:23
Code for SO Q: Random sample of character vector, without elements prefixing one another
sample0110 <- function(size, n, complete.only=FALSE) {
size <- as.integer(size)
n <- as.integer(n)
if(size > 25 || size < 3L) stop(
"Currently size min is 3 and max is 25, though should be possible to allow ",
"smaller and larger with some changes"
)
# Generate integer pool and weights
polygonizer <- function(x, outshape=NULL, gdalformat = 'ESRI Shapefile',
pypath=NULL, readpoly=TRUE, quietish=TRUE) {
# x: an R Raster layer, or the file path to a raster file recognised by GDAL
# outshape: the path to the output shapefile (if NULL, a temporary file will be created)
# gdalformat: the desired OGR vector format
# pypath: the path to gdal_polygonize.py (if NULL, an attempt will be made to determine the location
# readpoly: should the polygon shapefile be read back into R, and returned by this function? (logical)
# quietish: should (some) messages be suppressed? (logical)
if (isTRUE(readpoly)) require(rgdal)
if (is.null(pypath)) {
@etiennebr
etiennebr / as.data.table.r
Last active November 13, 2023 11:24
Transform raster or terra object to data.table
#' Transform raster to data.table
#'
#' @param x Raster* object
#' @param row.names `NULL` or a character vector giving the row names for the data frame. Missing values are not allowed
#' @param optional logical. If `TRUE`, setting row names and converting column names (to syntactic names: see make.names) is optional
#' @param xy logical. If `TRUE`, also return the spatial coordinates
#' @param centroids logical. If TRUE return the centroids instead of all spatial coordinates (only relevant if xy=TRUE)
#' @param sepNA logical. If TRUE the parts of the spatial objects are separated by lines that are NA (only if xy=TRUE and, for polygons, if centroids=FALSE
#' @param ... Additional arguments (none) passed to `raster::as.data.frame`
#'
@lptorres
lptorres / multi2single.py
Created December 4, 2013 01:55
A simple script that converts shapefiles with multipart polygons into singlepart polygons, and copies the fields of the source shapefile. This script is based on a script by Paolo Corti, found here: https://gist.github.com/anonymous/735330 The original script only converts multipolygons by creating a new feature for each part of a multipolygon. …
import os
from osgeo import gdal, ogr, osr
import sys
def initFields(in_lyr, out_lyr):
#Arbitrarily get the first feature
feat = in_lyr[0]
#loop over each field
@bearloga
bearloga / Parallel RJags with Progress Monitoring.R
Last active December 27, 2015 04:58
par.trace.samples() runs independent MCMC chains in parallel on a multicore/multiCPU system. It breaks up a single long run into several smaller chunks and that allows it to report the simulation's progress. At the end of the run, all the chunks are combined into a single mcmc object. The mcmc objects from the chains are then combined into a sin…
# This file contains two functions:
# - combine.samples (used by par.trace.samples)
# - par.trace.samples
# and an example at the bottom.
## Author: Mikhail Popov (mikhail [at] mpopov.com)
# install.packages("rjags") # JAGS must be installed on system
# install.packages("doMC") # Unix only
@wkmor1
wkmor1 / rasterToKML.r
Last active December 24, 2015 09:08
convert raster objects or raster readable files to .kml overlay files
rasterToKML <- function(x, out='Rgrid', png=FALSE, name=out, proj="+proj=longlat") {
require(raster)
require(maptools)
require(rgdal)
require(rgeos)
x <- raster(x)
SG <- as(x, 'SpatialGridDataFrame')
proj4string(SG) <- CRS(proj)
SG <- GE_SpatialGrid(SG)
if(png) png <- sprintf('%s.png', out) else png <- tempfile()