Skip to content

Instantly share code, notes, and snippets.

View gshotwell's full-sized avatar

Gordon Shotwell gshotwell

  • Halifax, Nova Scotia
View GitHub Profile
tbl.mock_db <- function(src, name, ...) {
return(src[[name]])
}
new_mock_db <-function(path) {
files <- list.files(path, full.names = TRUE)
con <- purrr::map(files, ~readr::read_csv(., col_types = classes[[basename(.)]]))
names(con) <- gsub(".csv$", "", basename(files))
class(con) <- c("mock_db", "list")
releasePackages <- function() {
dir.create("builds")
on.exit(unlink("builds", recursive = TRUE))
if (repository_head()$name != "master") {
stop("You can only release the master branch. Merge your changes into master before releasing.")
}
repo <- gitPath("repo")
released <- readRDS(file.path(repo, "src", "contrib", "PACKAGES.rds"))
@gshotwell
gshotwell / gist:75513ac4d42c38ca2d8880efebe81db9
Created July 17, 2019 15:35
Function to generate tests based on an object
match_vector <- function(vec) {
name <- deparse(substitute(vec))
length <- length(vec)
if (length < 10) {
identity_test <- glue::glue("expect_equal({name}, c(",
paste0(vec, collapse = ", "),
")")
}
expectations <- c(
glue::glue("expect_is({name}, 'vector')"),
library(tidyverse)
add <- function(...){
args <- eval(substitute(alist(...)))
args <- replaceMissingWithZero(args)
args %>% purrr::reduce(sum)
}
replaceMissingWithZero <- function(l){
out <- lapply(l, function(x){
if (rlang::is_missing(x)) {
> test_df6 <- create_test_df(cols=4, rows=40, levels_per_var= 6) # See appendix for 'create_test_df'
> g_test_df6 <- group_by(test_df6, a, b, c)
>
> base_split <- function(df)split(test_df6 , test_df6[, c('a', 'b', 'c')])
>
> microbenchmark::microbenchmark(
+ base_split(test_df6),
+ split(g_test_df6)
+ )
#Unit: milliseconds
@gshotwell
gshotwell / Substring.R
Created October 14, 2017 17:11
Try to figure out an O(n) algorithm to find all unique substrings
str <- "asdfasdf"
char <- data_frame(
letters = unlist(strsplit(str, "")),
skip = FALSE)
uniques <- c()
for (q in nchar(str):1) {
for (i in 1:(nchar(str) - q + 1)) {
start <- i
end <- i + q - 1
if (all( char$skip[start:end])) {
basic_output <- structure(list(name = "new_var", derivation = structure(list(
`function` = "case", args = list(structure(list(column = structure(1:3, class = "AsIs"),
type = structure(list(value = structure(list(class = "categorical",
categories = list(structure(list(id = 1L, name = "one",
numeric_value = NULL, missing = FALSE), .Names = c("id",
"name", "numeric_value", "missing")), structure(list(
id = 2L, name = "two", numeric_value = NULL,
missing = FALSE), .Names = c("id", "name", "numeric_value",
"missing")), structure(list(id = 3L, name = "three",
numeric_value = NULL, missing = FALSE), .Names = c("id",
`%d%` <- function(vars, list){
for(i in vars){
assign(i, list[[i]], envir = .GlobalEnv)
}
}
c("mpg", "cyl") %d% mtcars
test_list = list(cars = mtcars,
numbers = 1:500,
@gshotwell
gshotwell / gist:325632c1ba67e10e7951ecc220bc3da4
Created April 7, 2017 16:37
Women in Technology StackOverflow Sruvey
library(stacksurveyr)
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
library(ggplot2)
library(forcats)
df <- stack_survey %>%
select(respondent_id, gender, tech_do) %>%
@gshotwell
gshotwell / classify.R
Created December 16, 2016 14:46
I often want to match the classes between two data.frames, for instance when you lose column class information by moving between wide and long formats. Here is a convenience function to do that.
classify <- function(value, function_char){
fun <- get(paste0("as.", function_char))
fun(value)
}
mtcars_char[] <- map(mtcars_char, as.character)
map(mtcars_char, class)
mtcars_char[] <- map2(mtcars_char,
map_chr(mtcars, class),
~classify(.x, .y))