Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Redd halpo

View GitHub Profile
@halpo
halpo / margins.R
Created Sep 21, 2016
spread_each and margins functions which compliment dplyr and tidyr packages.
View margins.R
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 / 000-instructions.md
Created Jun 19, 2012
harvestr R users conference presentation.
View 000-instructions.md

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 / Rscript.reg
Created Mar 28, 2012
Setup commands for R files on windows
View Rscript.reg
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 Feb 28, 2012
Print a Table in markdown grid format.
View mdgridtable.R
#' 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 Feb 5, 2012
R Function Composition
View compose.R
#' 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 Jan 31, 2012
Compute statistics for a vector with renaming
View dostats.R
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 Dec 29, 2011
Texprint Example
View texprint.R
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 Dec 20, 2011
makes a graphviz plot of parsed R code from parser package
View parserGraph.R
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 / knitr.mk
Created Nov 29, 2011
knitr makefile
View knitr.mk
################################################################################
# Copyright 2011
# Andrew Redd
# 11/23/2011
#
# Description of File:
# Makefile for knitr compiling
#
################################################################################
all:pdf # default rule DO NOT EDIT
@halpo
halpo / Makefile
Created Nov 17, 2011
Makefile for R Packages
View Makefile
# 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)