Skip to content

Instantly share code, notes, and snippets.

View tylerlittlefield's full-sized avatar
🍓
🍋

Tyler Littlefield tylerlittlefield

🍓
🍋
View GitHub Profile
@tylerlittlefield
tylerlittlefield / les-miserables.R
Last active June 12, 2023 18:36
Les Miserables dataset for demonstrating echarts4r edge graph
library(echarts4r)
library(magrittr)
les <- jsonlite::fromJSON("https://gist.githubusercontent.com/tyluRp/0d7a53f2a1f55cb3c6ffe22c67618267/raw/0684a839c3e49dac1157721ddd906eff8f9491d4/les-miserables.json")
e_charts() %>%
e_graph(
layout = "circular",
circular = list(
rotateLabel = TRUE
@tylerlittlefield
tylerlittlefield / preserveSearch.js
Created April 1, 2021 02:57
Preserve search of selectizeInput
/**
* Plugin: "preserve_search" (selectize.js)
* Based on: "preserve_on_blur" of Eric M. Klingensmith
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
@tylerlittlefield
tylerlittlefield / keybase.md
Created March 5, 2021 04:31
Keybase identifier

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@tylerlittlefield
tylerlittlefield / scheduler.R
Created February 25, 2021 18:53
Return a vector of dates on a given day every n weeks
library(lubridate)
library(dplyr)
scheduler <- function(day = "monday", n_weeks = 2) {
x <- data.frame(date = seq.Date(as.Date("2021-01-01"), as.Date("2050-01-01"), by = "day"))
x$week <- ceiling(lubridate::week(x$date) / n_weeks)
x$day <- tolower(as.character(lubridate::wday(x$date, abbr = FALSE, label = TRUE)))
suppressWarnings({
x[x$day %in% day, ] %>%
@tylerlittlefield
tylerlittlefield / 538-to-db.R
Last active January 31, 2021 20:03
Take 538 data and push to database
# install.packages('fivethirtyeightdata', repos = 'https://fivethirtyeightdata.github.io/drat/', type = 'source')
# install.packages("fivethirtyeight")
# connect
con <- DBI::dbConnect(odbc::odbc(), "fivethirtyeight")
# get dataset names from each package
x <- tibble::as_tibble(data(package = "fivethirtyeight")$results)
y <- tibble::as_tibble(data(package = "fivethirtyeightdata")$results)
@tylerlittlefield
tylerlittlefield / compartment.R
Last active October 1, 2020 22:52
Experimental "compartment" or container for shiny UI
compartment <- function(title, ...) {
container_css <- stringr::str_squish(
"border: 3px solid orange;
position: relative;
border-radius: 8px;
padding-top: 20px;
font-family: 'Fira Sans', sans-serif;
margin-top: 25px;
margin-bottom: 25px;"
)
@tylerlittlefield
tylerlittlefield / org.json
Created July 13, 2020 16:34
JSON of an organization from AI2
{
"text": "ACME, Inc. Org Chart",
"nodeTypeToStyle": {
"root": ["color1", "strong"],
"1stlevel": ["color2"],
"2ndlevel": ["color3"],
"3rdlevel": ["color4"],
"4thlevel": ["color5"]
},
"linkToPosition": {
@tylerlittlefield
tylerlittlefield / compare.R
Created March 27, 2020 21:02
Compare sets
compare <- function(x, y) {
list(
"These values are in X but not Y" = setdiff(x, y),
"These values are in Y but not X" = setdiff(y, x),
"These values are shared between X and Y" = intersect(x, y),
"Combined, X and Y returns these values" = union(x, y)
)
}
@tylerlittlefield
tylerlittlefield / rlang_misc.R
Last active December 19, 2020 01:11
Changing the name in the lhs with rlang
library(rlang)
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 / context.R
Last active September 12, 2019 21:27
Grabbing the context of regular expression matches
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,