Skip to content

Instantly share code, notes, and snippets.

View jbryer's full-sized avatar

Jason Bryer jbryer

View GitHub Profile
@jbryer
jbryer / setFunctionParams.R
Created July 15, 2013 20:38
Set function parameters within the global environment. Useful for debugging R functions.
#' Set function parameters for debugging.
#'
#' Sets the defauls for the parameters in the given function witin an environment.
#'
#' @param fun the function whose parameters should be set.
#' @param envir the environment to set those parameters.
#' @param missing.value value to assign to parameters that do not have a defautl value.
#' @param overwrite should parameters already defined be overwritten.
setFunctionParams <- function(fun,
envir=.GlobalEnv,
@jbryer
jbryer / server.R
Last active December 22, 2017 20:16
Shiny Example of Gambler's Run
require(shiny)
require(shinyIncubator)
require(ggplot2)
theme_update(panel.background=element_blank(),
panel.grid.major=element_blank(),
panel.border=element_blank())
tickets <- as.data.frame(rbind(
c( '$1', 1, 15),
@jbryer
jbryer / getYearQuarter.R
Created April 18, 2013 12:00
Returns the quarter in which the date appears.
#' Returns the year (fiscal or calendar) and quarter in which the date appears.
#'
#' This function will cut the given date vector into quarters (i.e. three month
#' increments) and return an ordered factor with levels defined to be the quarters
#' between the minimum and maximum dates in the given vector. The levels, by
#' default, will be formated as \code{FY2013-Q1}, however the \code{FY} and \code{Q}
#' can be changed using the \code{fy.prefix} and \code{quarter.prefix} parameters,
#' respectively.
#'
#' @param x vector of type \code{\link{Date}}.
#' Convert a list of vectors to a data frame.
#'
#' This function will convert a list of vectors to a data frame. This function
#' will handle three different types of lists of vectors. First, if all the elements
#' in the list are named vectors, the resulting data frame will have have a number
#' of columns equal to the number of unique names across all vectors. In cases
#' where some vectors do not have names in other vectors, those values will be
#' filled with \code{NA}.
#'
#' The second case is when all the vectors are of the same length. In this case,
@jbryer
jbryer / parse.codebook.r
Last active March 9, 2023 15:19
Parses a codebook file where lines starting at column zero (far left) represet variable information (e.g. name, description, type) and indented lines (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor levels and labels.
#' Parse a codebook file with variable and level information.
#'
#' Parses a codebook file where lines starting at column zero (far left) represet
#' variable information (e.g. name, description, type) and indented lines
#' (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor
#' levels and labels.
#'
#' Note that white space at the beginning and end of each line is stripped before
#' processing that line.
#'
@jbryer
jbryer / xtable.decimal.r
Last active November 21, 2020 19:51
Prints a LaTeX table with numeric columns aligned on their decimal points. This function wraps the xtable and print.xtable functions in the xtable package so that numeric columns are aligned on their decimal place.
require(xtable)
#' Prints a LaTeX table with numeric columns aligned on their decimal points.
#'
#' This function wraps the \code{\link{xtable}} and \code{\link{print.xtable}}
#' functions in the \code{xtable} package so that numeric columns are aligned
#' on their decimal place.
#'
#' See \url{http://jason.bryer.org/posts/2013-01-04/xtable_with_aligned_decimals.html}
#' for more information.
@jbryer
jbryer / varEntryDialog.r
Created August 13, 2012 18:19
Function to create a tcl/tk dialog box for a user to enter variable values.
#' Creates a dialog box using tcl/tk to get input from the user.
#'
#' This function will create a tcl/tk dialog box to get user input. It has been
#' written to be extensible so the R programmer can easily create a dialog with
#' any number of varaibles with custom labels and data conversion of the user
#' entered data. The function will return a list where the element names are
#' \code{vars} and the value is the user input. By default, all entry will be
#' converted using the \code{as.character} function. However, this can easily be
#' altered using the \code{fun} parameter. For example, if integers are required,
#' use \code{fun=c(as.integer, ...)}. It is also possible to write custom
@jbryer
jbryer / ggplot2Cheat.r
Created April 26, 2012 19:35
Graphic Parameters (symbols, line types, and colors) for ggplot2
require(ggplot2)
require(grid)
theme_update(panel.background=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank())
#Borrowed (i.e. stollen) from http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.R
getColorHexAndDecimal <- function(color) {
if(is.na(color)) {
@jbryer
jbryer / Rprofile.R
Created March 7, 2012 16:15
My .Rprofile that works both on Windows and Linux
# .Rprofile -- commands in this file will be executed at the beginning of
# each R session. On Windows, the R_PROFILE environment variable must have value
# with the full path to this file. On Linux (or other Unix like systems) this file
# must be in the user's home directory.
# Set the default repository to the main CRAN site
options(repos=c(CRAN='http://cran.r-project.org'))
# Set the oDrive varaible and library path
if(Sys.info()['sysname'] == 'Windows') {
@jbryer
jbryer / setup.r
Created March 7, 2012 16:14
R Setup Script
#List of most used R packages that we wish to install.
libraries = c('cacheSweave', 'Deducer', 'devtools', 'doBy', 'foreign', 'gdata',
'ggplot2', 'Hmisc', 'JGR', 'lubridate', 'maps', 'mapdata', 'mapproj',
'maptools', 'proto', 'psych', 'R2wd', 'RCurl', 'reshape',
'RODBC', 'roxygen2', 'seqinr', 'sm', 'sp', 'sqldf', 'survey',
'WriteXLS', 'XML', 'xtable')
#We will install packages from the main CRAN site
repos = 'http://cran.r-project.org'
#Site provides some prebuilt binaries for Windows