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 / Makefile
Created November 17, 2011 20:07
Makefile for R Packages
# Makefile for generating R packages.
# 2011 Andrew Redd
#
# Assumes Makefile is in a folder where package contents are in a subfolder pkg.
# Roxygen uses the roxygen2 package, and will run automatically on check and all.
PKG_VERSION=$(shell grep -i ^version pkg/DESCRIPTION | cut -d : -d \ -f 2)
PKG_NAME=$(shell grep -i ^package pkg/DESCRIPTION | cut -d : -d \ -f 2)
R_FILES := $(wildcard pkg/R/*.R)
@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 / 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"