Skip to content

Instantly share code, notes, and snippets.

🍊
woof

Tyler Littlefield tyluRp

🍊
woof
Block or report user

Report or block tyluRp

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tyluRp
tyluRp / context.R
Last active Sep 12, 2019
Grabbing the context of regular expression matches
View context.R
library(stringr)
library(dplyr)
library(purrr)
library(rlang)
library(text2vec) # for movie reviews data
# favor stringr over base R because stringr handles NAs whereas base R returns
# zero length, base R below:
# regmatches(string, regexpr(x, string, ignore.case = ignore_case))
pattern_context <- function(string, pattern, n_before = 10, n_after = 10,
@tyluRp
tyluRp / draft_team_records.R
Created May 14, 2019
Draft Lottery Team Records
View draft_team_records.R
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") %>%
@tyluRp
tyluRp / rverbalexpressions_notes.R
Created Mar 21, 2019
Hypothetical re-write of RVerbalExpressions
View rverbalexpressions_notes.R
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
@tyluRp
tyluRp / convert_imgs.sh
Created Mar 18, 2019
Recursively convert images
View convert_imgs.sh
# 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
@tyluRp
tyluRp / rx_structure.R
Created Mar 17, 2019
Look at RVerbalExpressions Structure
View rx_structure.R
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",
@tyluRp
tyluRp / handle_null.R
Created Mar 12, 2019
Avoid arg = NULL in R functions
View handle_null.R
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")
@tyluRp
tyluRp / rx_literal2.R
Last active Mar 10, 2019
For question about vectorization
View rx_literal2.R
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" "@!"
@tyluRp
tyluRp / pkg_size.R
Last active Feb 12, 2019
Check the size of an R package or project
View pkg_size.R
# 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)
@tyluRp
tyluRp / describe.R
Last active Dec 20, 2018
Create a data.frame that describes each field
View describe.R
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) {
@tyluRp
tyluRp / resize_photos.md
Last active Dec 7, 2018
Recursively resize photos
View resize_photos.md

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.

You can’t perform that action at this time.