Skip to content

Instantly share code, notes, and snippets.

View daattali's full-sized avatar

Dean Attali daattali

View GitHub Profile
@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 / 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 / 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!")
}
# 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 / 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")) {
@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 / 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 / bcl-process.R
Created November 26, 2015 07:07
Process BC Liquor Store data
library(dplyr)
rawDataUrl <- "http://pub.data.gov.bc.ca/datasets/176284/BC_Liquor_Store_Product_Price_List.csv"
bcl <- read.csv(rawDataUrl, stringsAsFactors = FALSE)
products <- c("BEER", "REFRESHMENT BEVERAGE", "SPIRITS", "WINE")
bcl <- dplyr::filter(bcl, PRODUCT_CLASS_NAME %in% products) %>%
dplyr::select(-PRODUCT_TYPE_NAME, -PRODUCT_SKU_NO, -PRODUCT_BASE_UPC_NO,
-PRODUCT_LITRES_PER_CONTAINER, -PRD_CONTAINER_PER_SELL_UNIT,
-PRODUCT_SUB_CLASS_NAME) %>%
rename(Type = PRODUCT_CLASS_NAME,
Subtype = PRODUCT_MINOR_CLASS_NAME,
@daattali
daattali / 01_colourInput.R
Last active February 1, 2016 00:16
Little shiny apps used in my shinyjs tutorial http://bit.ly/shinyjs-slides
# See http://daattali.com/shiny/colourInput/ for more demos
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
colourInput("col", "Colour", "blue")
# colourInput("col", "Colour", "#ff0000",
# palette = "limited", showColour = "background")
@daattali
daattali / linkedin.R
Created March 5, 2016 11:11
Scraping Twitter and LinkedIn info in R
# Get a person's name, location, summary, # of connections, and skills & endorsements from LinkedIn
# URL of the LinkedIn page
user_url <- "https://www.linkedin.com/in/daattali"
# since the information isn't available without being logged in, the web
# scraper needs to log in. Provide your LinkedIn user/pw here (this isn't stored
# anywhere as you can see, it's just used to log in during the scrape session)
username <- "yourusername"
password <- "yourpassword"