Skip to content

Instantly share code, notes, and snippets.

View coolbutuseless's full-sized avatar

mikefc coolbutuseless

View GitHub Profile
#remotes::install_cran(c("anglr", "silicate"))

library(silicate)
library(anglr)
library(rgl)
## volcano is heightmap, doesn't exist in geo-space in R so we map it do the extent of
## these spatial polygons 
poly <- silicate::minimal_mesh
# xmin : 0 
@coolbutuseless
coolbutuseless / defaultlist.R
Last active May 12, 2020 10:58
defaultlist.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a list with a default value
#'
#' This behaves exactly like a 'list()' object, except if the requested value
#' does not exist, a default value is returned (instead of NULL).
#'
#' Similar to a `defaultdict` in Python
#'
#' @param value default value to return if item not in list
#'
@coolbutuseless
coolbutuseless / safe_system.R
Last active April 12, 2020 00:50
Is it possible to have a safe system2 call in R?
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Detect special characters in a character vector of args
#'
#' This is implemented as a whitelist of characters to accept. The presence
#' of anything outside this whitelist is considered a 'special character'
#'
#' @param args character vector of args to check
#'
@coolbutuseless
coolbutuseless / close-points.R
Created February 18, 2020 10:53
Find points close to an outline. Help needed!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# A bunch of points
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
N <- 10
point_coords <- cbind(
rep(seq(0, 1, length.out = N), times = N),
rep(seq(0, 1, length.out = N), each = N)
)
points <- sf::st_multipoint(point_coords)
@coolbutuseless
coolbutuseless / stack-alpha.R
Created February 13, 2020 10:39
stacking alpha
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simple function to create a plot with N overlapping rectangles with
# each having an alpha of 1/N
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stack_alpha <- function(N) {
plot_df <- tibble(
x = seq(0.45, 0.55, length.out = N),
y = seq(0.45, 0.55, length.out = N),
alpha = 1/N
)
@coolbutuseless
coolbutuseless / svg-fire.R
Created January 12, 2020 00:27
Simple fire animation in SVG with {minisvg}
library(minisvg)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define filter with turbulence driving the displacmenet
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_filter <- stag$filter(
id = "displacementFilter",
x = "-30%", y = "-30%", width="160%", height="160%",
stag$feTurbulence(
type = "turbulence",
@coolbutuseless
coolbutuseless / css.R
Created August 18, 2019 09:49
CSS helper
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a CSS ruleset
#'
#' Create a CSS ruleset consisting of a selector and one-or-more property declarations,
#' or, if no \code{.selector} is given, create an inline style string
#'
#' The list of included properties is not a complete list, but rather an
#' abbreviated list from
#' \url{https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference}
@coolbutuseless
coolbutuseless / many-args.R
Last active August 16, 2019 12:05
I came here for an argument.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simple demo of a 10 argument function
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nargs <- 10
f <- function() {}
fargs <- as.pairlist(setNames(rep(1, nargs), paste0('v', seq(nargs))))
formals(f) <- fargs
f
@coolbutuseless
coolbutuseless / npr.R
Created May 7, 2019 10:48
geom_streamline for some image manipulation
suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(metR)
library(tidyr)
})
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load a black and white Mona Lisa.
@coolbutuseless
coolbutuseless / 8-out-of-10-cats.R
Created February 2, 2019 01:14
Solving the numbers puzzle in "8 out of 10 cats does Countdown"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Inner recursive routine for solving Countdown numbers puzzle
#'
#' @param nums What numbers are left to select from?
#' @param value the current calculated value
#' @param expr the current readable expression
#' @param verbose output solutions as they are found? default: FALSE
#'
#' @return Character vector of solutions if any are found, otherwise NULL
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~