This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`%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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(stacksurveyr) | |
library(dplyr) | |
library(tidyr) | |
library(purrr) | |
library(stringr) | |
library(ggplot2) | |
library(forcats) | |
df <- stack_survey %>% | |
select(respondent_id, gender, tech_do) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |