Skip to content

Instantly share code, notes, and snippets.

View tylerlittlefield's full-sized avatar
🍓
🍋

Tyler Littlefield tylerlittlefield

🍓
🍋
View GitHub Profile
@tylerlittlefield
tylerlittlefield / replace_list.R
Last active November 16, 2018 20:18
Replace column values in each list element by column index
x <- list(iris[1:5, ], iris[1:5, ])
replace_list <- function(x, value, col) {
x[[col]] <- value
return(x)
}
lapply(x, replace_list, 7, 1)
#> [[1]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
@tylerlittlefield
tylerlittlefield / merged_prs.R
Created November 21, 2018 17:37
Gather and count all your merged pull requests
library(gh)
library(tidyverse)
library(jsonlite)
gh("/search/issues?q=author:tylurp+type:pr+is:merged") %>%
toJSON() %>%
fromJSON() %>%
.$items %>%
.$pull_request %>%
.$url %>%
@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",
@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 / 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 / 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 / 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 / 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_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 / 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