Skip to content

Instantly share code, notes, and snippets.

@expersso
expersso / convergence_in_distribution.R
Created October 18, 2018 15:01
Illustration of random sequence that convergences in distribution, but not density
library(tidyverse)
library(gganimate)
f <- function(n, x) 1 - cos(2 * pi * n * x)
g <- function(.f, x) map_dbl(x, ~integrate(function(z) .f(z), 0, .x)$value)
df <- cross_df(list(
x = seq(0, 1, length.out = 100),
n = c(1, 2, 3, 5, 10, 15, 25)
)) %>%
@expersso
expersso / movies.R
Created January 6, 2016 13:45
Movie budgets and box office success (1955-2015)
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
library(stringr)
library(rvest)
library(xml2)
library(OECD)
# Get budget and box office data
@expersso
expersso / extracting_html_table_w_attr.R
Last active April 18, 2024 19:01
Extracting HTML tables and arbitrary HTML attributes
# SPECIFIC PROBLEM: Extract an html table and include all links
# (even when there are no links or multiple links per cell)
#
# GENERAL PROBLEM: Extract an html table and include all attributes
# for any given tag and attribute
#
# Example usage at bottom of the page
#
# Motivation: https://twitter.com/daattali/status/717582654476914688
@expersso
expersso / rentswatch.R
Created May 20, 2016 14:20
Explorations in functional programming
library(httr)
library(purrr)
library(dplyr)
library(ggplot2)
library(scales)
library(ggthemes)
with_msg <- function(msg, f) {
function(...) {
@expersso
expersso / scrape_nfl.R
Last active June 7, 2021 00:01
Scraping NFL data with purrr and tidyr goodness
# Replicating https://t.co/Jq1QfFGpjA
library(rvest)
library(stringr)
library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)
get_and_clean_table <- function(url) {
library(dplyr); library(httr); library(rvest)
addr <- "Big Ben"
GET("http://maps.google.com/maps/api/geocode/xml",
query = list(address = addr)) %>%
content() %>%
html_nodes(xpath = "//location//lat | //location//lng") %>%
html_text() %>%
setNames(c("lng", "lat"))
@expersso
expersso / trigonometry.R
Created July 28, 2016 08:44
Trigonometry with magick, gganimate, and purrr
## Purpose: illustrate use of magick, gganimate, and purrr
## Inspiration:
## https://rud.is/b/2016/07/27/u-s-drought-animations-with-the-witchs-brew-purrr-broom-magick/
## and subsequent discussion on magick vs gganimate:
## https://twitter.com/hrbrmstr/status/758304420224466944
library(purrr)
library(ggplot2)
library(gganimate)
library(animation)
library(magick)
@expersso
expersso / python_v_r.R
Last active September 6, 2019 15:32
Wikipedia trend for Python and R
library(tidyverse)
library(scales)
library(stringr)
library(grid)
library(gridExtra)
library(wikipediatrend)
df <- wp_trend(c("R_(programming_language)", "Python_(programming_language)"),
from = "2007-12-01", to = "2017-01-12")
@expersso
expersso / ggplot2_DRY
Last active May 27, 2019 22:42
Example of using ggplot2's %+% to follow DRY principle
library(eurostat)
library(dplyr)
library(ggplot2)
library(scales)
#### Original version (not using %+%) ####
# Originally posted at http://www.r-bloggers.com/european-debt-and-interest/ on June 7, 2015
r1 <- get_eurostat('gov_10dd_edpt1')
@expersso
expersso / pairs_plot.R
Created October 29, 2018 16:26
Create scatterplot matrix using the tidyverse
library(tidyverse)
library(patchwork)
plot_pair <- function(data, x, y) {
ggplot(data, aes_string(x = x, y = y, color = "Species", shape = "Species")) +
geom_point() +
scale_color_brewer(palette = "Dark2") +
theme(legend.position = "none", plot.title = element_text(size = 7)) +
labs(x = NULL, y = NULL, title = paste0(y, " ~ ", x))