Skip to content

Instantly share code, notes, and snippets.

@PaulC91
PaulC91 / paris_trees.R
Created November 15, 2017 15:09
R script to chart and map Paris tree data
library(tidyverse)
library(lubridate)
library(stringr)
library(hrbrthemes)
library(sf)
library(leaflet)
# R script to chart and map Paris tree data
# data available here https://opendata.paris.fr/explore/dataset/les-arbres/ ----------------
@PaulC91
PaulC91 / sankey.R
Last active March 4, 2018 10:22
Example of using function to create d3 sankey diagram from multiple categorical variables + one numerical value variable of tidy data frame
library(tidyverse)
library(tidygraph)
library(igraph)
library(networkD3)
# https://www.kaggle.com/unitednations/refugee-data/data
asylum_seekers_raw <- read_csv("asylum_seekers.csv")
# get the top 10 countries of origin by 'Total decisions'
top_orig <- asylum_seekers_raw %>%
library(tidyverse)
library(sf)
#https://www.cultureofinsight.com/blog/2018/05/02/2018-04-08-multivariate-dot-density-maps-in-r-with-sf-ggplot2/
svk.dot <- st_read("shp/hranice_obce_3.shp", stringsAsFactors = FALSE, quiet = TRUE) %>%
st_transform(4326)
colnames(svk.dot)[1] <- "id"
@PaulC91
PaulC91 / gs_auth.R
Last active June 12, 2018 10:42
googlesheets setup for shiny
library(googlesheets)
# do this initially to save your token in the app's working direcotory
# once it's saved you can comment this code out
token <- gs_auth(cache = FALSE)
saveRDS(token, file = "googlesheets_shiny_token.rds")
# then this code in your app will authenticate and read in a workbook of your choice each time it's loaded
gs_auth(token = "googlesheets_shiny_token.rds")
sheet_key <- "your_google_sheet_key_here"
@PaulC91
PaulC91 / discogger_geo.R
Last active June 12, 2018 16:35
map your record collection with the discogger R package
library(discogger)
library(tidyverse)
library(sf)
library(rnaturalearth)
library(countrycode)
library(scico)
my_records <- discogs_user_collection("pacampbell91")
my_releases <- map_chr(my_records$content, "id") %>%
@PaulC91
PaulC91 / network_wc.R
Last active July 19, 2018 13:28
network graph of world cup players connected by club and country
library(rvest)
library(tidyverse)
library(tidygraph)
library(ggraph)
wc_squads <- html("https://en.wikipedia.org/wiki/2018_FIFA_World_Cup_squads")
tables <- wc_squads %>%
html_table()
@PaulC91
PaulC91 / render_decks.R
Created July 24, 2018 14:27
Create multiple parameterised powerpoint reports using rmarkdown and purrr
library(rmarkdown)
library(ggplot2)
library(purrr)
ppt_render <- function (data) {
params <- list(data = data)
group <- unique(data$class)
rmarkdown::render("report.Rmd", output_file = paste0("mpg_", group, ".pptx"), params = params)
}
@PaulC91
PaulC91 / babynames.R
Last active October 26, 2018 12:03
R code to create tidy data frame from ONS Baby names in England and Wales, 1996 to 2017 excel file
# Data available here
# https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/birthsdeathsandmarriages/livebirths/adhocs/009010babynames1996to2017englandandwales/adhocallbabynames1996to2017.xls
library(tidyverse)
library(readxl)
rank_names <- map_chr(2017:1996, paste0, "-rank")
count_names <- map_chr(2017:1996, paste0, "-count")
cnames <- c(rank_names, count_names) %>% sort(decreasing = TRUE)
@PaulC91
PaulC91 / make_gapminder_messy_again.R
Created August 21, 2018 15:44
R code to split the gapminder dataset across multiple tabs in an excel file by year
library(tidyverse)
library(gapminder)
library(openxlsx)
gm <- gapminder %>%
select(-continent)
# to get latest year as first tab in workbook
years <- unique(gm$year) %>% sort(decreasing = TRUE)
@PaulC91
PaulC91 / highcharter_axis_labelling.R
Last active February 27, 2019 19:42
maintain desired order of categorical xAxis regardless of data order
library(highcharter)
library(tibble)
df <- tibble(
group = c(rep("G1", 3), rep("G2", 4)),
cat = c("B", "C", "D", "A", "B", "C", "D"),
index = c(2, 3, 4, 1, 2, 3, 4),
n = runif(7)
)