Skip to content

Instantly share code, notes, and snippets.

View halpo's full-sized avatar

Andrew Redd halpo

View GitHub Profile
@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 / 000-instructions.md
Created June 19, 2012 16:40
harvestr R users conference presentation.

Building a beamer presentation with knitr.

Introduction

The documents included are the input for knitr. In addition you need to have the tool pandoc installed. I also use a custom beamer template to add the University of Utah \institute command to the template. It also changes the indentation some.

Steps

  1. knit document with
@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)
@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 / 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 / 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 / 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 / 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 / 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 / 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)