Skip to content

Instantly share code, notes, and snippets.

@alistaire47
alistaire47 / pMap.R
Created May 2, 2022 05:09
`purrr::pmap()` but with `Map()`-like semantics
pMap <- function(.l, .f, ...) {
.f <- rlang::as_function(.f, ...)
if (rlang::is_named(.l)) {
fun <- function(...) {
rlang::fn_env(.f) <- rlang::env(...)
.f(...)
}
} else {
fun <- .f
@alistaire47
alistaire47 / 2020-09-22_LA-RUG_Uncomfortable-data.R
Created September 22, 2020 19:00
2020-09-22 LA R Users Group talk: Uncomfortable data
#' Uncomfortable data
#' LA R Users Group
#' 2020-09-22
#' Edward Visel
#' Setup
library(tidyverse)
path <- '/tmp/nycflights13/flights.csv'
@alistaire47
alistaire47 / fugly-but-fast.R
Created August 27, 2019 01:16
brodie's challenge
alistaire <- function(n){
two_rows <- rep(seq_len(n/2L), each = 4L)
out_mat <- matrix(0L, n, n)
index_mat <- matrix(seq_len(n), nrow = 2L)
for (i in seq_len(n/2L)){
out_mat[index_mat[, i], ] <- two_rows
two_rows <- two_rows + 1L
}
out_mat
}
@alistaire47
alistaire47 / squircle.R
Created February 24, 2019 18:10
squircles
library(tidyverse)
library(gganimate)
animate(
crossing(
p = seq(0.1, 5, by = 0.1),
theta = seq(0, 2*pi, length.out = 101)
) %>%
mutate(r = 1 / (abs(cos(theta))^p + abs(sin(theta))^p)^(1/p)) %>%
ggplot(aes(theta, r, fill = p)) +
@alistaire47
alistaire47 / heart.R
Last active February 15, 2019 07:11
heart animation
library(tidyverse)
library(gganimate)
p <- crossing(t = (1:4)*pi/2,
x = seq(0, 2*pi, length = 501)) %>%
mutate(y = 2 - 2*sin(x) + sin(x) * sqrt(abs(cos(x)))/(sin(x) + 1.4),
x = (x + t) %% (2*pi)) %>%
ggplot(aes(x, y, color = x)) +
geom_path(size = 2, show.legend = FALSE) +
scale_color_gradient2(low = '#7a0177', mid = '#f768a1', high = '#fcc5c0') +
@alistaire47
alistaire47 / uptake-gif.R
Created November 28, 2018 04:36
uptake gifs
library(dplyr)
library(ggplot2)
library(gganimate)
### Horizontal rotation
p <- data_frame(x = c(-4, 0, 4), y = c(0, 3, 0), state = 1) %>%
bind_rows(mutate(., x = -x, state = 2)) %>% # frame flipped over y-axis
ggplot(aes(x, y, ymin = y, ymax = y + 1)) +
geom_ribbon() +
coalesce_join(
tribble(
~key, ~var1, ~var2,
'a', 1, NA,
'b', NA, 2
),
tribble(
~key, ~var1, ~var2,
'a', NA, 1,
'b', 2, NA
@alistaire47
alistaire47 / pythagorean_triples.Rmd
Created February 22, 2018 04:21
Pythagorean Triples
---
title: "Pythagorean Triples"
author: "Edward Visel"
output:
html_document:
df_print: paged
---
```{r}
library(tidyverse)
@alistaire47
alistaire47 / markdown.R
Last active October 18, 2018 19:41
read markdown table into R data frame
# base R version
read.markdown <- function(file, stringsAsFactors = FALSE, strip.white = TRUE, ...){
if (length(file) > 1) {
lines <- file
} else if (grepl('\n', file)) {
con <- textConnection(file)
lines <- readLines(con)
close(con)
} else {
lines <- readLines(file)
@alistaire47
alistaire47 / trump-score.R
Last active January 1, 2018 18:41
fivethirtyeight trump score
library(rvest)
library(tidyverse)
h <- read_html('https://projects.fivethirtyeight.com/congress-trump-score/house/')
trump_score <- h %>%
html_nodes('table.member-list') %>%
map(html_table, fill = TRUE) %>%
set_names(c('Senate', 'House')) %>%
map(set_names,