Skip to content

Instantly share code, notes, and snippets.

View csgillespie's full-sized avatar

Colin Gillespie csgillespie

View GitHub Profile
next_letter <- local({
i <- 0
function() {
i <<- i + 1
paste0("x_", c(letters, LETTERS))[i]
}
})
f <- function() {
new_arg <- alist(x = )
a <- 1
b <- 2
ftw <- function()
{
vars <- ls(envir = .GlobalEnv)
rm(list = vars, envir = .GlobalEnv)
}
ftw()
@daattali
daattali / beepr_error.R
Created May 11, 2015 01:58
beepr_error
# rasmusab/beepr package is great for telling you when your R code finishes running.
# But if the code throws an error, the beep will never come, and you won't know that
# it finished running until you visually check.
# Solution: change the error handler to a failure beep.
# Example
foo <- function(success = TRUE) {
if (!success) {
stop("Error!")
}
@leeper
leeper / plotthemes.R
Last active October 28, 2016 15:23
Experimenting with themes in base graphics
#' The goals here are to:
#' (1) play around with themes to make base graphics less ugly
#' (2) atomically specify multiple plot aspects (data, axes, grid, title) in a single call
#' (3) Recycle last plot theme to easily duplicate its appearance in the next plot
#' The idea is to create a set of customizable functions like `theme()`, which return a list of graphics arguments
#' These can then be passed to `splot`, which will cleanly handle them by passing some to `par` and some to `plot`
#' Different `theme()` functions can then be used to modify color palettes, axes, etc.
merge.list <-
function (x, y) {
@dsparks
dsparks / Visually-weighted regression.R
Created September 12, 2012 19:52
Visually-weighted regression plot, including simulation from a model
# A simple approach to visually-weighted regression plots
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "reshape2", "MASS")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some data:
nn <- 1000
myData <- data.frame(X = rnorm(nn),
@csgillespie
csgillespie / foundational-data-science.md
Last active October 31, 2019 16:55
Foundational data science with R

Software

Please make sure you are using a recent version of R (current version is 3.6.X) - the final digit is the minor version number. The essential numbers are the first two. You can check the version of R you are running via

R.version.string

Please install the latest version of RStudio (https://www.rstudio.com/products/rstudio/download/).

@gaborcsardi
gaborcsardi / cran-over-time.R
Last active July 18, 2020 19:21
Size of the CRAN R package repository over time
library(jsonlite)
## Download
pkgs <- fromJSON("http://crandb.r-pkg.org/-/events")
## Filter
na_pkgs <- unique(pkgs$name[ is.na(pkgs$date) ])
events <- pkgs[ ! pkgs$name %in% na_pkgs, c("date", "name", "event")]
@stephenturner
stephenturner / .Rprofile.r
Last active July 19, 2021 22:05
My .Rprofile
## See http://gettinggeneticsdone.blogspot.com/2013/06/customize-rprofile.html
## Load packages
library(BiocInstaller)
## Don't show those silly significanct stars
options(show.signif.stars=FALSE)
## Do you want to automatically convert strings to factor variables in a data.frame?
## WARNING!!! This makes your code less portable/reproducible.
@gadenbuie
gadenbuie / join-animations-with-gganimate.R
Last active January 11, 2022 15:48
Animated dplyr joins with gganimate
# Animated dplyr joins with gganimate
# * Garrick Aden-Buie
# * garrickadenbuie.com
# * MIT License: https://opensource.org/licenses/MIT
# Note: I used Fira Sans and Fira Mono fonts.
# Use search and replace to use a different font if Fira is not available.
library(tidyverse)
library(gganimate)
@emitanaka
emitanaka / collapseoutput.js
Created July 20, 2019 04:43
Collapsible Code Output for `xaringan`
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Output</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {