Skip to content

Instantly share code, notes, and snippets.

View djnavarro's full-sized avatar

Danielle Navarro djnavarro

View GitHub Profile
@djnavarro
djnavarro / bb_gapminder.R
Created February 26, 2019 08:14
draws the gganimate gapminder example, using bbc style
# packages
library(tidyverse)
library(gganimate)
library(gapminder)
library(scales)
# modified version of the bbc_style function
# adapted from: https://github.com/bbc/bbplot
bbc_style2 <- function(font = "Helvetica") {
theme(
@djnavarro
djnavarro / bayes_rain_viewer.R
Created March 12, 2019 03:48
Uses the asciify package to draw the "Bayesian rain" animation in the RStudio viewer pane
#devtools::install_github("djnavarro/asciify")
library(asciify)
library(magrittr)
temporary <- function(file) {
temp_dir <- tempfile()
dir.create(temp_dir)
file.path(temp_dir, file)
}
library(geojsonio)
library(here)
library(broom)
reload <- FALSE
if(reload) {
spdf <- geojson_read(
x = here("unsw_map", "worldgeo.json"),
what = "sp"
)
@djnavarro
djnavarro / a_very_bad_thing.R
Created March 25, 2019 09:45
Probably the most malicious thing I've done with R
# operator that reverses the direction of assignment
# only if called from the global environment
`%>>%` <- function(x,y) {
if(sys.nframe() == 1) {
assign(deparse(substitute(y)), x, parent.frame())
} else {
assign(deparse(substitute(x)), y, parent.frame())
}
}
@djnavarro
djnavarro / mouse_to_cat.R
Created March 29, 2019 06:27
Transform mouse emoji into cat emoji
library(tidyverse)
library(fontr)
library(emojifont)
library(ggpolypath)
library(transformr)
library(animation)
library(Cairo)
# create the base images from cat emoji and mouse emoji!
cat <- glyph_polygon(ch = emoji("cat"), family = "EmojiOne")
@djnavarro
djnavarro / transition_filter_bayes.R
Created April 17, 2019 06:32
gganimated version of ASCII Bayes
library(tidyverse)
library(asciify)
library(gganimate)
p <- ascii_data("bayes.png") %>%
ascii_map(
alphabet = as.character(0:9),
rescale = .15) %>%
ascii_plot(charsize = 4)
@djnavarro
djnavarro / coord_trans_example.R
Last active May 13, 2019 09:57
example where coord_trans throws many warnings
# from section 7.5.1 of ggplot2 book
library(ggplot2)
base <- ggplot(diamonds, aes(carat, price)) +
stat_bin2d() +
geom_smooth(method = "lm") +
xlab(NULL) +
ylab(NULL) +
theme(legend.position = "none")
# authenticate using OSF token stored in .Renviron
osfr::osf_auth()
# define download function
download_project <- function(node, path) {
proj <- osfr::osf_retrieve_node(node)
files <- osfr::osf_ls_files(proj)
if(!dir.exists(path)) dir.create(path)
for(i in 1:nrow(files)) {
cat("downloading: ", files$name[i], "\n")
@djnavarro
djnavarro / pipe_bitch.R
Created July 3, 2019 21:08
source code to "pipe bitch"
pipe_bitch <- tibble::tibble(
line = 1:80,
lyric = c(
"you wanna?",
"you wanna?",
"",
"you wanna code tidy?",
"you wanna R party?",
"you wanna be Shiny?",
"you better pipe bitch",
@djnavarro
djnavarro / censor.R
Last active July 10, 2019 02:41
quick and dirty code for systematic censoring
library(tidyverse)
# define the censoring function
censor_smoking <- function(df) {
# (this code is awful I am so sorry)
df <- df %>%
rowwise() %>% # ugh... rowwise
mutate(keep_case = case_when(
(sex.factor == "Male" & runif(1) < .3) ~ 0, # censor male with prob .3