Skip to content

Instantly share code, notes, and snippets.

View jbryer's full-sized avatar

Jason Bryer jbryer

View GitHub Profile
@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 / 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 / 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 / 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:
#
@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 / 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
library(tidyverse)
library(reshape2)
library(lubridate)
library(rnoaa)
token <- '' # Get token here: https://www.ncdc.noaa.gov//cdo-web/token
options(noaakey = token)
marathon_dates <- c('2021-01-10','2020-01-12','2019-01-13',
'2018-01-07','2017-01-08','2016-01-10','2015-01-11','2014-01-12',
@jbryer
jbryer / package.R
Last active January 16, 2022 06:31
#' Simplified loading and installing of packages
#'
#' This is a wrapper to \code{\link{require}} and \code{\link{install.packages}}.
#' Specifically, this will first try to load the package(s) and if not found
#' it will install then load the packages. Additionally, if the
#' \code{update=TRUE} parameter is specified it will check the currently
#' installed package version with what is available on CRAN (or mirror) and
#' install the newer version.
#'
#' @param pkgs a character vector with the names of the packages to load.
@jbryer
jbryer / app.R
Last active February 28, 2022 15:51
Framework for running Shiny Applications from R packages with parameters passed to the application. See this blog post for more information: https://bryer.org/post/2021-02-12-shiny_apps_in_r_packages/
# Run the App on a Shiny Server
library(shiny)
source('shiny_param_test.R')
shiny::shinyApp(ui = shiny_ui,
server = shiny_server)