Skip to content

Instantly share code, notes, and snippets.

@egnha
egnha / get_pkg_info.R
Last active July 31, 2016 11:48
Get R package information from CRAN
#' Get R package information from CRAN
#'
#' \code{get_pkg_info()} retrieves the description of a CRAN package; it is
#' adapted from \url{http://developer.r-project.org/CRAN/Scripts/depends.R}.
#' Results are cached per R session using the
#' \href{https://cran.r-project.org/web/packages/memoise/index.html}{memoise}
#' package.
#'
#' @param pkg Package name (as a string)
#' @param fields Package description fields (as a vector); it corresponds to
@egnha
egnha / flatten_list.R
Last active April 4, 2017 14:31
Fully flatten a nested list into a (named) list of leaves
#' Automatically assign missing names in a list or vector
#'
#' \code{fill_in_names()} supplies missing names in a list or vector according
#' to the following rule: if component \code{n} of \code {x} is missing, then it
#' is given the name \code{paste0(nm_prefix, n)}, where the prefix string
#' \code{nm_prefix} has the default value \code{..} (to reduce the chance of
#' conflicts with existing names).
#'
#' @param x A list or vector.
#' @param nm_prefix A string that is used to prefix generated names.
@egnha
egnha / plot_labels_lazyeval.R
Last active December 20, 2016 06:09
Resolve plot labels using lazyeval
# Fully resolve plot labels using lazyeval
# Get the body of a function with values in place of object names
body_text <- function(f) {
nms <- as.list(environment(f), all.names = TRUE)
deparse(lazyeval::interp(body(f), .values = nms))
}
# Functionality of body_text() implemented in base R
body_text_oldschool <- function(f) {
@egnha
egnha / unpack.R
Last active May 2, 2017 09:36
Python-style tuple unpacking for lists (Gabor Grothendieck)
# Python-style tuple unpacking for lists (Gabor Grothendieck)
#
# unpack[...] is syntactic sugar for R that enables you to do Python-style
# tuple unpacking for lists. This is handy when a function returns multiple
# values in the form of a list, and you want assign those values to
# individual names.
#
# NB: As in Python, unpacking is done by position.
#
# Reference: https://stat.ethz.ch/pipermail/r-help/2004-June/053343.html
@egnha
egnha / try_return_error.R
Last active May 2, 2017 09:40
Blanket tryCatch for all errors
# Won't pass R CMD check because of call to internal function
do_try_catch <- function(expr, env) {
.Internal(.addCondHands("error", list(identity), env, environment(), FALSE))
expr
}
# Return value of expression, or error object in case of failure
try_return_error <- function(expr) {
env <- parent.frame()
value <- do_try_catch(return(expr), env)
@egnha
egnha / R.css
Last active October 11, 2017 05:16
CSS for RStudio Help pane
/*
* R.css
*
* Copyright (C) 2009-16 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
@egnha
egnha / partial.R
Last active July 15, 2017 06:21
Partial function application
#' Partial function application
#'
#' @param ...f Function to partially apply.
#' @return Function of `...` that partially applies `...f`. (However, if no
#' argument values are set, `...f` is simply returned.)
#'
#' @details `partial()` resolves an error in [purrr::partial()], see purrr
#' issue [349](https://github.com/tidyverse/purrr/issues/349). The `.env`,
#' `.lazy`, `.first` arguments are dropped.
#'
@egnha
egnha / .bash_prompt
Created July 25, 2017 08:30
Bash prompt with Git branch status
RESET="\[\033[0m\]"
BLUE="\[\033[01;34m\]"
GREEN="\[\033[1;32m\]"
YELLOW="\[\033[0;33m\]"
prompt_command () {
PS_BRANCH=""
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
PS_BRANCH=" (git:${ref#refs/heads/})"
}
@egnha
egnha / compose.R
Last active August 9, 2017 05:22
Signature-preserving function composition
# Signature-preserving function composition
library(rlang)
# Variation of purrr::compose() that:
# - matches the formals to that of the first called function
# - supports unsplicing of a list of functions
compose <- function(...) {
fs <- lapply(dots_list(...), as_function)
n <- length(fs)
@egnha
egnha / eshell.el
Created August 30, 2017 09:47
Open eshell in split window
;; http://bit.ly/2iIFUNd
(setq display-buffer-alist '(("\\`\\*e?shell" display-buffer-pop-up-window)))