Skip to content

Instantly share code, notes, and snippets.

View fdschneider's full-sized avatar

Florian Schneider fdschneider

View GitHub Profile
plot_palette <- function(v) {
pardefault <- par()
call <- format(substitute(v))
n <- length(v)
par(mar = c(0,0,0,0)+0.05, bty = "n")
plot(NA,NA, type = "n", xaxt = "n", yaxt = "n", xlim = c(0,1), ylim = c(0,1),
asp =1/10)
rect(xleft = seq(0,1,1/n)[1:n],
#' BExIS access over a Webservice
#'
#' Read table from a web service. Inherits functionality of `read.table()`.
#'
#' @param datasetid Integer BExIS ID of the requested dataset.
#' @param user User name on BExIS. If not provided, function will prompt input.
#' @param pswd Password on BExIS. If not provided, function will prompt input.
#' @param dec the character used in the file for decimal points.
#' @param na.strings a character vector of strings which are to be interpreted as NA values. Blank fields are also considered to be missing values in logical, integer, numeric and complex fields.
#' @param fill logical. If TRUE then in case the rows have unequal length, blank fields are implicitly added. See 'Details' of `?read.table`.
@fdschneider
fdschneider / get_gbif_taxonomy.R
Created May 11, 2017 10:12
Function to extract accepted names and taxonomy from GBIF Backbone Taxonomy
#' Get accepted canonical names and taxonomy for a given species name
#'
#' @param x a character string or vector of species names.
#' @param infraspecies logical. If TRUE, the given name is resolved to infraspecies taxon, i.e. subspecies names will not be pooled (not working!)
#' @param fuzzy logical. If TRUE, function tries fuzzy matching for species requests. May produce output, if normal matching fails. (not working!)
#' @param verbose logical. If FALSE, warnings and messages are suppressed.
#'
#' @return a data.frame of accepted taxon names and higher taxa.
#' @import taxize
#' @import data.table
@fdschneider
fdschneider / highlight.r
Created July 25, 2016 06:57
transfer numerical vector into color gradient
highlight <- function(x, colrange = c("black", "red2"), steps = NULL, range = "auto"){
if(is.null(steps)) steps = length(unique(x))
if(range[1] == "auto") {
min_val = min(x, na.rm = TRUE)
max_val = max(x, na.rm = TRUE)
} else {
min_val = range[1]
max_val = range[2]
@fdschneider
fdschneider / patches.r
Created July 25, 2016 06:55
identify and count the size of individual patches in a binary matrix
# identify and count the size of individual patches
patches <- function(x, state) {
pattern <- x$cells
pattern <- pattern %in% state
map <- rep(NA, times = prod(x$dim))
old <- rep(99, times = prod(x$dim))
while(!identical(old[pattern], map[pattern])) {
old <- map