Skip to content

Instantly share code, notes, and snippets.

View potterzot's full-sized avatar

Nicholas Potter potterzot

View GitHub Profile
@potterzot
potterzot / plot_multi_libraries.md
Created August 4, 2016 17:28
Plotting with multiple library options in R
#' Example plot for this question 
#' @export
#' 
#' @param ... functions passed to plot functions for specific engines. 
#' @param engine character determining which plotting engine to use.
ex_plot <- function(..., engine = c("ggplot", "plotly")) {
  engine <- match.arg(engine)
  p <- if(engine == "ggplot") { ex_plot.ggplot(...) }
@potterzot
potterzot / tidy_data_pipes.md
Created December 15, 2015 19:59
Example of pipes in tidyr and dplyr
library(tidyr)
library(dplyr)

#fake data in wide form
temperatures <- data.frame(
  date = as.Date('2009-01-01') + 0:9,
  am = rnorm(10, 0, 1),
  pm = rnorm(10, 0, 2),
  midnight = rnorm(10, 0, 4)
@potterzot
potterzot / make.sh
Created December 4, 2015 01:51
Bash script to automate creating latex files for phd applications
#!/bin/bash
#===========
# Setup
#VARIABLES
out_dir=./submission
src=~/schools/applications/src
cv_file=~/professional/cv.pdf
@potterzot
potterzot / R_bokeh_ggplot_ellipses.md
Created October 28, 2015 18:24
How to modify and pass variable arguments within functions, and setting different plotting engines
##Example of allowing multiple plotting engines and optional layers
library(rbokeh)
library(ggplot2)


myplot <- function(..., engine = c("ggplot2", "rbokeh")) {
  engine <- match.arg(engine)
  
  #allow for "all" option in layers
@potterzot
potterzot / fmatch_compare.md
Created October 27, 2015 17:35
Comparison of which.min() and fmatch(min())
library(microbenchmark)

dat <- matrix(rnorm(10000000), nrow = 10000)

tdat <- matrix(rnorm(1000), nrow=100)


#Base method
microbenchmark(apply(dat,1,which.min), times=10L)
@potterzot
potterzot / README.md
Last active September 27, 2015 19:07
Convert large zipped csv files to compressed RDS files for use in R.

##Convert zipped csv files to gzipped RDS files for use in R.

Note that at the moment, while we can read in in chunks and save without a problem, and the file size grows with each chunk, that the final RDS object only contains the first chunk saved. The connection stays open and so the writes of later chunks continue to happen, but aren't loaded when you load the RDS file. If someone knows of a way to make this work I'd love to hear about it!

Convert zipped csv files that are too large to read directly into memory in R into RDS files, which are equivalent in size to a zipped csv and can be read in directly and much more quickly in R. A 2GB csv file can crash things on your laptop, but with this function you can convert it to a ~250MB RDS file and read that directly with no problem.

The function uses chunks to avoid reading a lot of data into R at once. Right now the number of chunks is limited to the letters in the alphabet, so csv files larger than 10 or GB might not work well, but then you should pro

@potterzot
potterzot / README.Rmd
Last active September 17, 2015 21:40
Reproducible examples in R
```
devtools::install_github("jennybc/reprex")
library(reprex)
reprex({mean(1:4)})
```
If you get an error like:
>pandoc: loadlocale.c:131: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_COLLATE) / sizeof (_nl_value_type_LC_COLLATE[0]))' failed.
Error in reprex_(r_file, venue, show, upload.fun) :
@potterzot
potterzot / README.md
Created September 10, 2015 19:14
Steps to create an sqlite3-compatible EC2 instance.

##Amazon EC2 and simple web server deployment

Often I want to create a simple web site that is nearly static with the exception of a simple SQL database (mostly to serve data of some form, occassionally to allow collecting it). These days with static data I'd probably do something like mongodb, but legacy has it's effects...

Anyway, steps (see a guide like this one:

  1. Spin up a micro instance of the free variety
  2. Install updates (sudo yum update) and httpd (sudo yum install httpd)
  3. Add a security rule to allow http access from the web
  4. Edit /etc/httpd/conf/httpd.conf to change DocumentRoot AllowAccessFrom to All
@potterzot
potterzot / getGEFSForecast.Rmd
Last active December 26, 2017 19:52
Fetch GEFS forecast data using netCDF.
---
title: "Get GEFS Forecast Data"
author: "Nicholas Potter"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = TRUE, include = TRUE, echo = TRUE)
```
@potterzot
potterzot / index.html
Last active August 27, 2015 15:19
Create a simple web comic with p5js.
<!DOCTYPE html>
<html>
<head>
<title>p5js Example</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.8/p5.js"></script>
<body>
<h1>Processing.js Test</h1>
<p>Using p5js to create simple canvas comic graphics... (sorry for the embarrassingly bad joke.)</p>
</body>