Skip to content

Instantly share code, notes, and snippets.

mikefc coolbutuseless

Block or report user

Report or block coolbutuseless

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View css.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' 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 Aug 16, 2019
I came here for an argument.
View many-args.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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
geom_streamline for some image manipulation
View npr.R
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 Feb 2, 2019
Solving the numbers puzzle in "8 out of 10 cats does Countdown"
View 8-out-of-10-cats.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' 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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@coolbutuseless
coolbutuseless / interleave.Rmd
Created Jan 31, 2019
interleave matrix and vector
View interleave.Rmd
```{r results='hide'}
vec <- c(101, 102, 103)
mat <- matrix(c( 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12), nrow = 4, byrow = TRUE)
```
```{r}
@coolbutuseless
coolbutuseless / list-comprehensions.R
Created Jan 24, 2019
Python-ish list comprehensions in R [Proof of concept]
View list-comprehensions.R
library(purrr)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Proof-of-concept
# Python style list comprehensions in R
# [ expression for item in list if conditional ]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lc <- 'dummy'
class(lc) <- 'listcomprehension'
`[.listcomprehension` <- function(...) {
@coolbutuseless
coolbutuseless / nested.R
Last active Oct 2, 2018
alternatives to nested for loops
View nested.R
check <- function(dog, location) { cat("Checking for", dog, "in", location, "\n") }
dogs <- c('poodle', 'greyhound', 'mutt')
locations <- c('park', 'street', 'yard')
@coolbutuseless
coolbutuseless / memoise-with-size-limit.R
Created Sep 24, 2018
memoise in rstats with a limit on how large an object can be in the cache
View memoise-with-size-limit.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A version of 'memoise::memoise' with limits on individual object size
#'
#' @param f Function of which to create a memoised copy.
#' @param ... optional variables specified as formulas with no RHS to use as
#' additional restrictions on caching. See Examples for usage.
#' @param envir Environment of the returned function.
#' @param cache Cache function.
#' @param object_size_limit maximum size of objects stored in cache.
#' Default: 1048576 bytes (1MB)
@coolbutuseless
coolbutuseless / is_within.R
Created Sep 20, 2018
Stricter membership testing in #rstats
View is_within.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A strict version of '%in%' where both the in-group and out-group must be completely specified
#'
#' The membership test is strict.
#' - if 'universe' is defined, then `outgroup = setdiff(universe, ingroup)`
#' - Every value of 'x' must exist within either 'ingroup' or 'outgroup'
#' - 'ingroup' and 'outgroup' must be disjoint sets
#' - May specify only one of 'outgroup' or 'universe'
#'
@coolbutuseless
coolbutuseless / strict_case_when.R
Created Sep 20, 2018
Stricter version of dplyr::case_when()
View strict_case_when.R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Stricter version of case_when()
#' - disallows a fall-through 'TRUE' value on the LHS.
#' - disallows input values which do not match any rules.
#' - disallows input values which match more than one rule
#'
#' @param ... arguments to case_when
#'
#' @return A vector of length 1 or n, matching the length of the logical input
#' or output vectors, with the type (and attributes) of the first RHS.
You can’t perform that action at this time.