Skip to content

Instantly share code, notes, and snippets.

View nathancday's full-sized avatar

Nathan (Nate) Day nathancday

View GitHub Profile
@nathancday
nathancday / split_.R
Created March 4, 2018 20:08
A tidyverse incarnation of split()
library(rlang)
library(tidyverse)
split_ <- function(data, ..., .drop = TRUE) {
vars <- ensyms(...)
vars <- map(vars, function(x) eval(x, data))
split(data, vars, drop = .drop)
}
mtcars %>% split_(cyl)
@nathancday
nathancday / iris_boxplots_no_outliers.R
Last active April 18, 2018 13:26
Help_for_ShStudent.R
# https://stackoverflow.com/questions/49880268/ggplot-how-to-remove-the-outliers-from-multiple-boxplots?noredirect=1#49880268
library(tidyverse)
data(iris)
iris_long <- gather(iris, "key", "val", -Species)
# with the outliers
ggplot(iris_long, aes(Species, val)) +
geom_boxplot() +
function export_gcal_to_gsheet(){
// fill with your details
var mycal = "you@email.com";
var cal = CalendarApp.getCalendarById("email.com@hakjshfkhflsafkshfklaj@group.calendar.google.com");
// interval of interest
var events = cal.getEvents(new Date("August 1, 2018 00:00:00 CST"), new Date("August 31, 2018 23:59:59 CST"));
var sheet = SpreadsheetApp.getActiveSheet();
# https://en.wikipedia.org/wiki/Fizz_buzz
values <- 1:15
rules <- c("fizz" = 3, "buzz" = 5)
container <- rep("", length.out = length(values))
for (name in names(rules)) {
rule_idx <- values %% rules[name] == 0
@nathancday
nathancday / gtfs_sf.R
Last active March 31, 2019 18:06
library(gtfs) %>% library(sf)
library(sf)
library(magrittr)
library(tidyverse)
gtfs_routes_sf <- function(gtfs) {
## gather key-values first ----
# trips_df has route_id:shape_id
shape_key <- gtfs$trips_df %>%
@nathancday
nathancday / inputMap.R
Last active May 6, 2019 15:00
Using a leaflet map as Shiny input widget
library(shiny)
library(sf)
library(leaflet)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ui <- fluidPage(
titlePanel("inputMap in Shiny"),
sidebarLayout(
sidebarPanel(
library(scales)
library(magrittr)
library(tidyverse)
theme_set(
ggthemes::theme_fivethirtyeight(base_size = 10) +
theme(legend.position = "right",
legend.direction = "vertical",
axis.title = element_text())
)
con <- file("test.log", "at")
sink(con, append=T, type = "message")
con <- file("test_sleep100.log", "at")
sink(con, append=T, type = "message")
library(purrr)
map(1:100,
possibly(function(x) {
@nathancday
nathancday / weighted_sampling_scratch.R
Created October 24, 2022 11:47
Code snippet for demo and graphic in Probability-Proportional-To-Size-Sampling blogpost
#' ---
#' title: Code snippet for demo and graphic in Probability-Proportional-To-Size-Sampling blogpost
#' author: nathancday--at-Github--
#' date: 2022-10-24
#' ---
# Libs --------------------------------------------------------------------
library(cowplot)
library(tidyverse)
@nathancday
nathancday / outline_demo.R
Created December 25, 2019 16:46
Open in Rstudio to see a outline demo for organizing your Rscripts
#' ---
#' title: The best title
#' author: You
#' date: 2019-12-25
#' ---
library(magrittr)
library(tidyverse)
# Import ------------------------------------------------------------------