Skip to content

Instantly share code, notes, and snippets.

View daattali's full-sized avatar

Dean Attali daattali

View GitHub Profile
@daattali
daattali / bcl-data.csv
Created November 26, 2015 06:55
BC Liquor Store data
Type Subtype Country Name Alcohol_Content Price Sweetness
WINE TABLE WINE RED CANADA COPPER MOON - MALBEC 14 30.99 0
WINE TABLE WINE WHITE CANADA DOMAINE D'OR - DRY 11.5 32.99 0
WINE TABLE WINE RED CANADA SOMMET ROUGE 12 29.99 0
WINE TABLE WINE WHITE CANADA MISSION RIDGE - PREMIUM DRY WHITE 11 33.99 1
WINE TABLE WINE RED UNITED STATES OF AMERICA ZINFANDEL - BIG HOUSE CARDINAL ZIN 13.5 36.99 0
WINE TABLE WINE RED FRANCE LE VILLAGEOIS RED - CELLIERS LA SALLE 11 34.99 0
WINE TABLE WINE RED CANADA SAWMILL CREEK - MERLOT 12.5 119 0
WINE TABLE WINE WHITE CANADA SOLA 12 32.99 0
WINE TABLE WINE WHITE CANADA GANTON & LARSEN PROSPECT - PINOT BLANC BIRCH CANOE 2011 11.5 13.99 0
@daattali
daattali / quiet.R
Created September 25, 2015 00:35
Suppress all output from an expression, cross-platform
#' Suppress all output from an expression. Works cross-platform.
#' @param expr Expression to run.
#' @param all If \code{TRUE} then suppress warnings and messages as well;
#' otherwise, only suppress printed output (such as from \code{print} or
#' \code{cat}).
#' @keywords internal
#' @export
quiet <- function(expr, all = TRUE) {
if (Sys.info()['sysname'] == "Windows") {
file <- "NUL"
@daattali
daattali / app.R
Last active October 11, 2022 09:04
Basic form-submission shiny app used in "Persistent data storage in shiny apps" article http://deanattali.com/blog/shiny-persistent-data-storage/
library(shiny)
# Define the fields we want to save from the form
fields <- c("name", "used_shiny", "r_num_years")
# Save a response
# ---- This is one of the two functions we will change for every storage type ----
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responses")) {
# Suppose a function has an argument "x". If a "child function" has multiple
# argument beginning with "x" and I want to pass the param "x" to its parent,
# I get error: `argument n matches multiple formal arguments`
# The reason this happens is clear, but at 3am my brain is firing blanks
# trying to find a solution. Help!
library(magrittr)
foo <- function(x, ...) UseMethod("foo")
foo.parent <- function(x, n = 5, ...) n
@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!")
}
@daattali
daattali / S3_parent_params.R
Created May 8, 2015 03:12
S3 method does not show parent params in autocomplete
# There are two humans: `human` and `angry_human`. They only differ in the fact
# that angry humans have a frown on their face.
# Example code:
# dean <- human("Dean")
# ex <- angry_human("Ex")
# plot(dean)
# plot(ex)
# When plotting `dean`, RStudio shows me all the available parameters.
@daattali
daattali / closest_colour.R
Last active March 7, 2016 12:53
Find the R built-in colour that's closest in RGB space to a given colour
# Find the R built-in colour that's closest in RGB space to a given colour
# This can be useful if you have a colour in mind you want to use in your R
# code, but would rather use a pretty built-in name rather than a HEX value.
#
# @param target The target RGB colour, given as a length-3 numeric vector
# @param n The number of top hits to return
# @param superset A vector with all possible colour names to choose from
#
# Note that the closest colour in RGB space is not guaranteed to be the
# closest colour visually, but it works in many cases.
@daattali
daattali / spinme
Created February 1, 2015 03:16
Add this at top of R script so that "source"ing the script will automatically render a markdown+HTML
#+ spinme-chunk, eval = FALSE, echo = FALSE
.scriptName <- basename(sys.frame(1)$ofile)
.spinOutput <- knitr::spin(.scriptName, knit = TRUE)
stop(sprintf("Ignore this error - your output is ready at:\n%s",
normalizePath(.spinOutput)),
call. = FALSE)
#+ spinme-done, eval = TRUE, echo = TRUE
```{r include = FALSE}
opts_chunk$set(tidy = FALSE)
```
Terrorism in Israel
===========================
**Dean Attali**
**Oct 4, 2013**
**STAT 545A**
```{r include = FALSE}
opts_chunk$set(tidy = FALSE)
```
Dean Attali
=================================
**STAT 545A hw 4**
**Sept 28 2013**
In this assignment, I will take data aggregation code written by other students, and create visuals that will complement their tabular data. The two students' work I will be using are [**Rebecca Johnston**](http://rpubs.com/rljohn/stat545a-2013-hw03_johnston-reb) and [**Daniel Dinsdale**](http://rpubs.com/danieldinsdale/stat545a-2013-hw03_dinsdale-dan).