default <- function(sym) {
Sym <- substitute(sym)
if (!(is.symbol(Sym)))
stop('\nExpected a symbol as an argument, got a ', class(sym))
ArgList <- eval.parent(quote(formals()))
ArgName <- as.character(Sym)
if (!(ArgName %in% names(ArgList)))
stop('\nThere is no argument named `', ArgName,'`')
if (is.symbol(ArgList[[ArgName]]))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
factorsToDummies <- function(df) | |
# df: a data.frame. | |
# returns: a data.frame with non-factor columns unchanged | |
# and factor columns replaced by a series of dummies | |
# for each factor. | |
lapply(df, | |
function(x) | |
if (x %>% is.factor %>% not) x else |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
# A more efficient implementation avoiding | |
# expand.grid which copies the parameters/arguments multiple times, | |
# which is inefficient for large parameters (e.g. data.frames). | |
mlapply <- function(.Fun, ..., .Cluster=NULL, .parFun=parallel::parLapply) { | |
`--List--` <- | |
list(...) | |
names(`--List--`) <- | |
names(`--List--`) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
cond <- function(...) { | |
# Clojure-style cond macro in R -- creates nested if-else calls | |
# arguments: pairs -- condition1, what-if-true1, | |
# condition2, what-if-true2, | |
# etc... | |
# what-if-all-contitions-false | |
e <- parent.frame() | |
substitute(list(...)) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
# Clojure-style cond and case macros in R | |
# Inspired by | |
# https://clojuredocs.org/clojure.core/cond and | |
# https://clojuredocs.org/clojure.core/case. | |
# See the examples in the bottom. | |
cond_case_Factory <- function(ENV, obj, message_infix, vectorised, comparFun, ...) { | |
IF <- if (vectorised) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
stopifnot('digest' %in% installed.packages()[,"Package"]) | |
if (!dir.exists('.cache.db')) dir.create('.cache.db') # in the current working directory | |
# Public API -------------------------------------------------------------- | |
cachedCall <- | |
function(`fun*`, | |
..., # file paths/names in args in ... need to be wrapped in File() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
split_into <- function(x, n, sorted=TRUE) | |
## x -- a vector (atomic or list) | |
## n -- the number of elements (groups) | |
## returns a list with n elements | |
## each containing some of the elements of x | |
n %>% | |
seq_len %>% | |
rep.int(x %>% | |
length %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(cacheflow) # https://github.com/alekrutkowski/cacheflow | |
CCA <- function(FUN,...) | |
## cachedCall&Assign | |
## A macro to avoid repetitive typing like | |
## myX_result <- cachedCall(myX_function, ...) | |
## creates and object `.myX` to avoid overwriting function `myX` | |
## that still may be used in the subsequent cachedCall/CCA calls | |
## i.e. | |
## CCA(myX, ...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
# requires also `eurodata` package | |
# https://github.com/alekrutkowski/eurodata | |
# Helpers ----------------------------------------------------------------- | |
msg <- function(obj, txt='\n') { | |
message(txt) | |
obj |
OlderNewer