Skip to content

Instantly share code, notes, and snippets.

View smbache's full-sized avatar
Pro

Stefan Milton Bache smbache

Pro
View GitHub Profile
@smbache
smbache / arm
Created June 26, 2014 08:08
Fun with pipe and armed expressions
#' Arm a value with a function which fires when evaluated.
#'
#' @param expr an expression
#' @param fun a function of one argument.
#'
#' @return an expression which when evaluated returns the result of expr,
#' after evaluating fun(expr).
arm <- function(expr, fun)
{
substitute({
@smbache
smbache / NamesTest
Last active August 29, 2015 14:04
Toying with declarations of symbols for use with non-standard evaluation.
#' Placeholder function.
#'
#' @export
as_nonstandard <- function()
{
stop("This is a placeholder function, and should not be called directly.")
}
#' Declare symbols for nonstandard evalution.
#'
`%<<%` <- function(con, x)
{
UseMethod("%<<%", x)
}
`%<<%.function` <- function(con, x)
{
if (!identical(x, close))
stop("Unknown function provided to <<", call. = FALSE)
library(dplyr) # These two need that it is magrittr@dev that is installed.
library(magrittr) # i.e. the dev branch @ GitHub, not master.
library(ggplot2)
library(mgcv)
"http://zevross.com/blog/wp-content/uploads/2014/08/chicago-nmmaps.csv" %>%
read.csv(as.is = TRUE) %>%
mutate(date = as.Date(date)) %>%
filter(date > as.Date("1996-12-31")) %>%
mutate(year = substring(date, 1, 4)) %T>%
@smbache
smbache / trigger.R
Last active August 29, 2015 14:10
trigger function
#' Trigger an action associated with first matched/valid condition.
#'
#' trigger is a flavour of pattern matching (or an if-else abstraction) in which a
#' value is matched against a sequence of condition-action sets. When a valid
#' match/condition is found the action is triggered and the result of the action
#' is returned. The trigger function is designed to particularly useful in pipelines
#' ala magrittr.
#'
#' @param value the value to match agaist
#' @param ... a set of formulas containing a condition as LHS and an action as RHS.

To -> or not to ->

In the blog post "A Step to the Right in R Assignments", by @hrbrmstr, and in later twitter discussions (e.g. here), it is argued that <- (and %<>% for that matter) is "illogical" and feels awkward.

Although I can see where the temptation comes from, I consider -&gt; to be

# Allow for V(g) %$% color or g %>% V %$% color
#
# Since %$% uses the generic `with` method, it is possible to tweek this to
# deal with special cases like this. I know little about igraph, but this
# may work.
#
with.igraph.vs <- function(data, expr, ...) {
eval(substitute(data$c, list(c = substitute(expr))))
}
@smbache
smbache / copy_from.R
Created April 2, 2015 19:28
Example of a function to copy functions from a package.
# a few utility funcs, might as well take them from `import`
import::here(symbol_list, symbol_as_character, .from = import)
copy_from <- function(.from, ...)
{
symbols <- symbol_list(...)
parent <- parent.frame()
from <- symbol_as_character(substitute(.from))
for (s in seq_along(symbols)) {
@smbache
smbache / loggrex.R
Last active August 29, 2015 14:19
A loggr example
library(loggr) # should be executed before sourcing the script
log_file("all.log")
log_file("messages.log", DEBUG, INFO, .warning = FALSE, .error = FALSE)
log_file("console", WARN, ERROR, CRITICAL, .message = FALSE)
top_n <- function(data_set, sort_var, n = 3, desc = FALSE)
{
if (!is.data.frame(data_set))
log_critical("Invalid data_set provided.")
@smbache
smbache / stringterpolate.R
Last active March 15, 2016 08:57
An approach to string interpolation in R
###
### This is an approach to string interpolation in R.
### First a few utility functions, then the actual function (stringterpolate): A shorter name
### is probably desirable. Finally a few examples to show its use.
###
#' Utility Function for Matching a Closing Brace
#'
#' Given positions of opening and closing braces \code{match_brace} identifies
#' the closing brace matching the first opening brace.