Skip to content

Instantly share code, notes, and snippets.

View halpo's full-sized avatar

Andrew Redd halpo

View GitHub Profile
@halpo
halpo / time.R
Created November 16, 2011 18:27
Time formatting for proc_time in R
#' formats proc_time as a "hh:mm:ss" time string
#'
#' @seeAlso print.proc_time, proc.time
#'
formatTime <- function(x){
stopifnot(inherits(x, "proc_time"))
h <- x %/% 3600L
m <- (x %% 3600L) %/% 60L
s <- x %% 60L
ifelse(is.na(x), NA, sprintf("%02d:%02d:%2.2f",h,m,s))
@halpo
halpo / Makefile
Created November 16, 2011 21:45
Makefile for Sweave with pgfSweave/cacheSweave
################################################################################
all:pdf # default rule DO NOT EDIT
################################################################################
MAINFILE := main
RNWFILES :=
RFILES :=
TEXFILES :=
CACHEDIR := cache
FIGUREDIR := figures
DRIVER := pgfSweave
@halpo
halpo / parserGraph.R
Created December 20, 2011 22:46
makes a graphviz plot of parsed R code from parser package
library(Rgraphviz)
library(plyr)
parserGraph <- function(x){
d0 <- attr(x, 'data')
d <- mutate(d0, id =as.character(id), parent=as.character(parent))
stopifnot(inherits(x, "parser"))
g <- new("graphNEL", nodes=as.character(d$id), edgemode='directed')
d_ply(d, .(id), with, {
if(parent %in% d$id)
g <<- addEdge(id, parent, g, 1)
@halpo
halpo / texprint.R
Created December 29, 2011 18:34
Texprint Example
library(xtable)
texprint<-new.env(parent=.GlobalEnv)
texprint$print<-function(x, ...){
UseMethod("texprint")
}
texprint$texprint.default <- function(x, ...){
UseMethod("print")
}
@halpo
halpo / dostats.R
Created January 31, 2012 17:45
Compute statistics for a vector with renaming
library(plyr)
library(stringr)
#' Convenient interface for computing statistics on a vector
#' @author Andrew Redd
#'
#' @param x the vector
#' @param ... statistics to compute, must take a vector and return a vector
#' @param .na.action the action to take on NA values, for all statistics
#'
#' @return A one row \code{data.frame} with columns named as in \code{...}
@halpo
halpo / compose.R
Created February 5, 2012 00:03
R Function Composition
#' Copyright Andrew Redd 2012
#' Licensed under GPLv3 or newer
#' Nest functions
#' @return new function cosisting of the functions nested
compose <- function(..., .list){
l <- if(missing(.list)) {
list(...)
} else {
.list
@halpo
halpo / mdgridtable.R
Created February 28, 2012 06:27
Print a Table in markdown grid format.
#' Print a table with text borders.
#' @param df a data.frame or matrix
#'
#' Prints a table with borders of |, -, +, and = which is usefull for
#' formating tables to use with markdown.
#'
#' @importFrom stringr str_c str_length str_dup
#' @importFrom dostats compose
#' @export
#' @examples
@halpo
halpo / Rscript.reg
Created March 28, 2012 19:21
Setup commands for R files on windows
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.R]
@="Rsource"
[HKEY_CLASSES_ROOT\.Rout]
@="Routput"
[HKEY_CLASSES_ROOT\Rsource]
@="R Source"
@halpo
halpo / knitr.mk
Created November 29, 2011 18:55
knitr makefile
################################################################################
# Copyright 2011
# Andrew Redd
# 11/23/2011
#
# Description of File:
# Makefile for knitr compiling
#
################################################################################
all:pdf # default rule DO NOT EDIT
@halpo
halpo / margins.R
Created September 21, 2016 21:27
spread_each and margins functions which compliment dplyr and tidyr packages.
margins <-
function( grouped #< [grouped_df] A data frame with groups defined.
, ... #< passed to `FUN`
, FUN=dplyr::summarize #< Summary function
, all.name = getOption("margins::all.name", "(All)")
){
"Add margins to summarization"
g <- groups(grouped)
com <- lapply(seq(0, length(g)), combn, x=g, simplify=FALSE)
com <- Reduce(c, com)