Skip to content

Instantly share code, notes, and snippets.

View wjhopper's full-sized avatar

Will Hopper wjhopper

View GitHub Profile
library(rstudio.prefs)
use_rstudio_prefs(
save_workspace = "never", # General --> Basic --> Save Workspace
load_workspace = FALSE, # General --> Basic --> Restore .RData
restore_last_project = FALSE, # General --> Basic --> Restore most recent project
restore_source_documents = FALSE, # General --> Basic --> Restore previously open source documents
check_for_updates = FALSE, # General --> Basic --> Check for updates
if (.Platform$OS.type == "windows") {
folder <- file.path(Sys.getenv("AppData"), "RStudio", "templates")
} else {
folder <- "~/.config/rstudio/templates"
}
if (!dir.exists(folder)) { dir.create(folder, recursive = TRUE) }
f <- file(file.path(folder, "default.Rmd") ,"wt")
@wjhopper
wjhopper / clean_CCLabEM_backups.sh
Created June 9, 2017 15:19
Cleaning old CCLab Experiment Manager backups
#! \bin\bash
## This script is to help clean up the experiment manager backups
cd /media/PatricksHD/CC9_Backups
touch removal_list.txt
# Compressed tarballs of the pydio user data directory are taken twice a day with a cron job
# The filenames follow the naming convention pydio_userdata_backup_YYYY-MM-DD_HH-MM.tar.bz
# Thus, this script assumes that the date of a backup can be extacted by splitting
@wjhopper
wjhopper / subscramble_nse.R
Created May 9, 2017 19:55
Examining R's Non-Standard Evaluation Behavior with subscramble
library(pryr)
sample_df <- data.frame(a = 1:5, b = 5:1, c = c(5, 3, 1, 4, 1))
scramble <- function(x) x[sample(nrow(x)), ]
subset1 <- function(x, cond) {
condition_call <- substitute(cond)
r <- eval(condition_call, x, parent.frame())
x[r, ]
@wjhopper
wjhopper / apply_vs_for.R
Created May 9, 2017 16:57
apply vs for-loop benchmark in R
#The task: sum 1000 different 10x10 matrices stored in a list
matrices <- vector(mode="list", length=1000)
for (i in seq_along(matrices)) {
matrices[[i]] <- matrix(rnorm(1000), 10, 10)
}
f1 <- function () {
S <- matrix(0, 10, 10)
for (M in matrices) {
@wjhopper
wjhopper / log_scale_methods.R
Created May 9, 2017 16:53
Log-Scale Axis with ggplot2
library(ggplot2)
library(scales)
library(gridExtra)
set.seed(20)
n <- 10
y <- rexp(n, 2)
dat <- data.frame(
xval = rep(c("A","B"), n/2),
yval = y,

Keybase proof

I hereby claim:

  • I am wjhopper on github.
  • I am whopper (https://keybase.io/whopper) on keybase.
  • I have a public key whose fingerprint is B703 3199 97F5 7774 B0C9 4FAC 7048 A20E F958 73DF

To claim this, I am signing this object:

@wjhopper
wjhopper / metaprogramming.R
Created May 9, 2016 13:32
Metaprogramming in R
# Create a function programmatically by creating its constituents:
# an argument list, a function body of expressions, and an enclosing environment
args <- alist(x=,y=)
exps <- expression(z <- x^2 + y^2, z <- sqrt(z), return(z))
body <- as.call(c(as.name("{"), exps))
f <- as.function(x = c(args,body), envir = parent.frame())
f(x=1,y=1)
@wjhopper
wjhopper / demographics.m
Created December 11, 2015 13:57
Demographic Info for Experiments
%--------------------------------------------------------%
% Onscreen script to record race/ethnic/sex demographics %
% for Matlab %
% Updated 09/21/2015 %
%--------------------------------------------------------%
%{
Purpose:
A Matlab script which will generate a set of dialog boxes that ask
participants an assortment of questions regarding demographics (in
@wjhopper
wjhopper / functional_madness.R
Created November 29, 2015 20:48
Observe the amazing obscurity of 100% "functional" R code!
## Some sort of list comprehension using only functional programming
## techniques that returns a list holding the coefficients of variation
## (i.e. the mean divided by the standard deviation) for each column in the
## mtcars data set.
## This isn't the clearest code (i.e. its really hard to tell its dividing
## the mean by the standard deviation) but it uses absolutely no imperative control
## flow structures and no vectorized R functions, so if you can read and understand
## this you can do you some functional programming for great good =). I can't believe I
## actually figured this out, much less that I figured it out in like 5 minutes,