Skip to content

Instantly share code, notes, and snippets.

View jbryer's full-sized avatar

Jason Bryer jbryer

View GitHub Profile
@jbryer
jbryer / Login.R
Last active September 24, 2020 10:35
# This script is modified by Jason Bryer (jason@bryer.org) from Huidong Tian's
# original script. The blog post describing the method is here:
# http://withr.me/authentication-of-shiny-server-application-using-a-simple-method/
# The original R script is located here: https://gist.github.com/withr/9001831
#
# This script adds two new features: 1. Render a logout button, and 2. provide
# the ability for visitors to create a new account.
#
# Within your server.R file, be sure to use:
#
#' 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 / 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}}.
@jbryer
jbryer / DisneyMarathonWeather.R
Last active October 2, 2018 17:36
Disney Marathon Weather in ggplot2
library(ggplot2)
library(reshape2)
weather <- read.csv('DisneyMarathonWeather.csv')
weather.melt <- melt(weather[,c('Year', 'Low', 'High', 'Wind', 'StartHumidity', 'Sky')],
id.vars = c('Year', 'Wind', 'StartHumidity', 'Sky'))
ggplot(weather.melt, aes(x = Year)) +
geom_ribbon(data = weather, aes(ymin = Low, ymax = High), alpha = 0.3, fill = 'skyblue') +
geom_path(aes(y = value, group = variable)) +
@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 / 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 / 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 / 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 / BirthdayProblem.R
Created January 31, 2012 20:29
Given a room with n people in it, what is the probability any two will have the same birthday?
## See http://en.wikipedia.org/wiki/Birthday_problem for an explanation of the problem
require(ggplot2)
require(reshape)
theme_update(panel.background=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank())
birthday <- function(n) {
1 - exp( - n^2 / (2 * 365) )