Skip to content

Instantly share code, notes, and snippets.

View robertzk's full-sized avatar

Robert Krzyzanowski robertzk

View GitHub Profile
@robertzk
robertzk / ternary.R
Last active June 19, 2021 02:40
ternary operator in R
# A ternary operator in R.
if (!is.element("super", installed.packages()[,1])) { devtools::install_github("robertzk/super") }
`?` <- function(expr1, expr2) {
if (missing(expr2)) {
super::super(expr1)
} else {
expr2 <- substitute(expr2)
if (!(is.call(expr2) && identical(expr2[[1]], as.name(":")))) {
super::super(expr1, expr2)
@robertzk
robertzk / Github-PAT-Help.md
Last active September 28, 2017 21:36
Help with setting up Github PAT token

Ensure you have a Github account. Next, visit the Github help page to set up your Github auth token.

Make sure git is installed. Open up your terminal, and ensure git is installed by running which git (if you do not see git not found, then you have to do nothing). If you do not have git, run brew install git. Next, configure your git identity.

Finally, copy the authenatication token you obtained earlier, and then run the following line from your terminal (with the token replaced by your token):

echo "export GITHUB_PAT=c1e2feed3242cfa0ab2e560ce4f380f66c6e9547" >> ~/.bash_profile && source ~/.bash_profile
@robertzk
robertzk / lunchnlearn-073017.md
Last active July 31, 2017 20:02
Lunch & Learn 07/30/17: R environments
env <- new.env()

d <- 1
fn <- function(a, b) {
  a + b + d
}

fn2 <- (function() {
  d <- 2
@robertzk
robertzk / r-python.md
Last active April 7, 2016 16:20
R-Python itertools rosetta stone

A translation of Python's itertools to R.

count(10) # 10 11 12 13 14 ...
count <- function(x, step = 1) {
  function() { (x <<- x + step) }
}
@robertzk
robertzk / capture_stack_trace.R
Created January 19, 2016 23:30
Capture an R stack trace
capture_calls <- function(e) {
e$calls <- sys.calls()
signalCondition(e)
}
fn <- function(x) {
call_another_function(x + 1)
}
call_another_function <- function(y) {
@robertzk
robertzk / example_fn_aliasing.R
Created January 16, 2016 00:36
Example of function aliasing
for (i in 1:10) {
assign(paste0("fn", i), eval(bquote(function(x) { x + .(i) })))
}
#' This is my function.
#'
#' This does stuff.
#'
#' @param x
#' @return foo.
def mystery_method(x)
->(z) do
x.inject(z) { |f, v| f(v) }
end
end
@robertzk
robertzk / exists.r
Created December 6, 2013 22:48
R exercise: Write your own version of exists(inherits = FALSE) (Hint: use ls()). Write a recursive version that behaves like inherits = TRUE. http://adv-r.had.co.nz/Environments.html
exists <- function(name, env = parent.frame(), inherits = TRUE) {
if (identical(env, emptyenv())) return(FALSE)
if (name %in% ls(envir = env)) TRUE
else if (inherits) exists(name, parent.env(env))
else FALSE
}
@robertzk
robertzk / fget.r
Created December 6, 2013 22:41
R exercise: Write a function called fget() that finds only function objects. It should have two arguments, name and env, and should obey the regular scoping rules for functions: if there's an object with a matching name that's not a function, look in the parent. (This function should be a equivalent to match.fun() extended to take a second argum…
fget <- function(name, env = parent.frame(), inherits = TRUE) {
if (identical(env, emptyenv())) stop(name, ' not found')
if (exists(name, env = env, inherits = FALSE) && is.function(tmp <- env[[name]])) return(tmp)
else if (inherits) fget(name, parent.env(env))
else stop(name, ' not found')
}
@robertzk
robertzk / get.r
Last active December 30, 2015 12:48
R exercise: Write your own version of get() using a function written in the style of where(). http://adv-r.had.co.nz/Environments.html
get <- function(name, env = parent.frame()) {
if (identical(env, emptyenv())) stop(name, ' not found')
if (exists(name, env = env, inherits = FALSE)) return(env[[name]])
else get(name, parent.env(env))
}