Last active
December 14, 2015 18:09
-
-
Save austinogilvie/5127075 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # To source this file into an environment to avoid cluttering the global workspace, put this in Rprofile: | |
| # my.env <- new.env(); sys.source("C:/PathTo/THIS_FILE.r", my.env); attach(my.env) | |
| #----------------------------------------------------------------------- | |
| # Load packages, set options and cwd, set up database connection | |
| #----------------------------------------------------------------------- | |
| ## Load packages | |
| library(RPostgreSQL) | |
| library(grid) | |
| library(ggplot2) | |
| library(reshape2) | |
| library(directlabels) | |
| library(plyr) | |
| library(scales) | |
| library(lubridate) | |
| # db.driver <- dbDriver('PostgreSQL') | |
| # db.host <-'odc_dw_prod' | |
| # db.username <-'foo' | |
| # db.password <- 'bar' | |
| # db.name <- 'gp_production' | |
| # greenplum <- dbConnect(db.driver, host=db.host, user=db.username, password=db.password, dbname=db.name) | |
| ## Sets the working directory to C:/R | |
| working.directory <- "/hernamesbarbara/Workspace/R" | |
| setwd(working.directory) | |
| message("\n******************************************************************************************") | |
| message(paste("Set working directory to", working.directory), sep=" ") | |
| message("\n******************************************************************************************") | |
| ## Don't show those silly significanct stars | |
| #options(show.signif.stars=FALSE) | |
| ## dont convert strings to factor by default (e.g. in read.csv) | |
| options(stringsAsFactors=F) | |
| ## Hard code the US repository for CRAN so it doesn't ask me every time. | |
| r <- getOption("repos") | |
| r["CRAN"] <- "http://cran.us.r-project.org" | |
| options(repos = r) | |
| rm(r) | |
| #----------------------------------------------------------------------- | |
| # Functions | |
| #----------------------------------------------------------------------- | |
| ## Transpose a numeric data frame with ID in first column | |
| ix.transpose <- function(d) { | |
| row.names(d) <- d[[1]] | |
| d[[1]] <- NULL | |
| d <- as.data.frame(t(d)) | |
| d$id <- row.names(d) | |
| d <- cbind(d[ncol(d)], d[-ncol(d)]) | |
| row.names(d) <- NULL | |
| d | |
| } | |
| ## Convert selected columns of a data frame to factor variables | |
| factorize.columns <- function(d, ...) lapply(d, function(x) factor(x, ...)) | |
| ## Returns names(df) in single column, numbered matrix format. | |
| n <- function(df) matrix(names(df)) | |
| ## Single character shortcuts for summary() and head(). | |
| s <- base::summary | |
| h <- utils::head | |
| ## ht==headtail, i.e., show the first and last 10 items of an object | |
| ht <- function(d) rbind(head(d,10),tail(d,10)) | |
| ## Show the first 5 rows and first 5 columns of a data frame or matrix | |
| hh <- function(d) d[1:5,1:5] | |
| ## Open current directory on mac | |
| mac.o <- function(...) system("open .") | |
| ## Takes a dataframe and a column name, and moves that column to the front of the DF. | |
| column.front <- function(d=dataframe, colname="colname") { | |
| index <- match(colname, names(d)) | |
| cbind(d[index],d[-index]) | |
| } | |
| read.file <- function(f) { | |
| q <- readLines(f, warn=F) | |
| return (paste(q, collapse='\n')) | |
| } | |
| ## Multiplot utility function for ggplot viewports | |
| # Multiple plot function | |
| # | |
| # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) | |
| # - cols: Number of columns in layout | |
| # - layout: A matrix specifying the layout. If present, 'cols' is ignored. | |
| # | |
| # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), | |
| # then plot 1 will go in the upper left, 2 will go in the upper right, and | |
| # 3 will go all the way across the bottom. | |
| # | |
| multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { | |
| require(grid) | |
| # Make a list from the ... arguments and plotlist | |
| plots <- c(list(...), plotlist) | |
| numPlots = length(plots) | |
| # If layout is NULL, then use 'cols' to determine layout | |
| if (is.null(layout)) { | |
| # Make the panel | |
| # ncol: Number of columns of plots | |
| # nrow: Number of rows needed, calculated from # of cols | |
| layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), | |
| ncol = cols, nrow = ceiling(numPlots/cols)) | |
| } | |
| if (numPlots==1) { | |
| print(plots[[1]]) | |
| } else { | |
| # Set up the page | |
| grid.newpage() | |
| pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) | |
| # Make each plot, in the correct location | |
| for (i in 1:numPlots) { | |
| # Get the i,j matrix positions of the regions that contain this subplot | |
| matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) | |
| print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, | |
| layout.pos.col = matchidx$col)) | |
| } | |
| } | |
| } | |
| # Did you make it this far? | |
| message("\n******************************\nSuccessfully loaded Rprofile.r\n******************************") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment