Skip to content

Instantly share code, notes, and snippets.

View tylerlittlefield's full-sized avatar
🍓
🍋

Tyler Littlefield tylerlittlefield

🍓
🍋
View GitHub Profile
@tylerlittlefield
tylerlittlefield / draft_team_records.R
Created May 14, 2019 22:22
Draft Lottery Team Records
library(tidyverse)
library(janitor)
library(glue)
library(rvest)
df <- "https://en.wikipedia.org/wiki/2019_NBA_draft" %>%
read_html() %>%
html_table(fill = TRUE, header = FALSE) %>%
.[[6]] %>%
as_tibble(.name_repair = "unique") %>%
@tylerlittlefield
tylerlittlefield / rverbalexpressions_notes.R
Created March 21, 2019 17:51
Hypothetical re-write of RVerbalExpressions
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
@tylerlittlefield
tylerlittlefield / convert_imgs.sh
Created March 18, 2019 20:38
Recursively convert images
# src: https://superuser.com/questions/71028/batch-converting-png-to-jpg-in-linux
find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; done
@tylerlittlefield
tylerlittlefield / rx_structure.R
Created March 17, 2019 18:10
Look at RVerbalExpressions Structure
library(tidyverse)
funcs <- ls("package:RVerbalExpressions")
df <- enframe(funcs, NULL, "func") %>%
mutate(
type = case_when(
func == "%>%" ~ "utility",
func == "rx" ~ "utility",
func == "rx_alpha" ~ "character class",
@tylerlittlefield
tylerlittlefield / handle_null.R
Created March 12, 2019 16:28
Avoid arg = NULL in R functions
handle_null <- function(.data) {
switch(
as.character(missing(.data)),
"TRUE" = NULL,
"FALSE" = .data
)
}
# so we can have docs like this
rx_anything(.data, mode = "greedy")
@tylerlittlefield
tylerlittlefield / rx_literal2.R
Last active March 10, 2019 19:50
For question about vectorization
rx_literal2 <- function(.data = NULL, ... ) {
stopifnot(
length(unique(sapply(list(...), length))) == 1
)
args <- sapply(list(...), function(x) if(inherits(x, "rx_string")) x else sanitize(x))
apply(args, 1, paste, collapse="")
}
rx_literal2(c("!", "@"), c("abc", "!"))
#> [1] "!abc" "@!"
@tylerlittlefield
tylerlittlefield / pkg_size.R
Last active February 12, 2019 21:56
Check the size of an R package or project
# Check Package Size
#
# Thanks to Alan Dipert for the help with this one.
#
# This function is used to calculate size of package and report size in
# README.md
pkg_size <- function(package) {
root <- find.package(package)
rel_paths <- list.files(root, all.files = TRUE, recursive = TRUE)
abs_paths <- file.path(root, rel_paths)
@tylerlittlefield
tylerlittlefield / describe.R
Last active December 20, 2018 21:09
Create a data.frame that describes each field
df <- data.frame(
x1 = letters[1:10],
x2 = 1:10,
x3 = sample(c(TRUE, FALSE), 10, replace = TRUE),
x4 = rnorm(10),
x5 = as.factor(LETTERS[1:10]),
stringsAsFactors = FALSE
)
describe <- function(x, class = TRUE) {
@tylerlittlefield
tylerlittlefield / resize_photos.md
Last active December 7, 2018 20:44
Recursively resize photos

Make sure to cd into the directory with subfolders containing photos, for example:

#> λ ~/ cd Desktop/test
#> λ ~/Desktop/test/ ls
#> 100003 100004

Where 100003 and 100004 are folders that contain photos. If the folders don't exist and you need to rename photos based on the file name, see this unix solution or this windows solution.

@tylerlittlefield
tylerlittlefield / tidylangs.R
Created November 27, 2018 04:43
Plot the language composition of tidyverse core packages
library(gh)
library(tidyverse)
library(jsonlite)
tidyrepos <- c("ggplot2",
"dplyr",
"tidyr",
"readr",
"purrr",
"tibble",