Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / purrr_ppt_maker.R
Last active March 2, 2019 17:17
R code to generate multiple powerpoint decks with native ppt charts from a single dataset using officer and purrr
library(tidyverse)
library(officer)
library(mschart)
# function to make ppt deck for each class in mpg data
new_deck <- function (i) {
data <- mpg %>%
filter(class == i)
ppt_chart <- ms_scatterchart(data, x = "displ", y = "hwy", group = "cyl") %>%
@PaulC91
PaulC91 / purrr_ppt.R
Last active October 2, 2019 23:18
Example of programming powerpoint slides using purrr::walk and the officer package
library(tidyverse)
library(officer)
library(mschart)
# create new ppt with title slide
mypres <- read_pptx() %>%
add_slide(layout="Title Slide", master="Office Theme") %>%
ph_with_text(type = "ctrTitle", str = "Make slide decks with purrr & officer") %>%
ph_with_text(type = "subTitle", str = "example using mtcars data")
@PaulC91
PaulC91 / wc_network.R
Created June 14, 2018 11:03
network graph of world cup players countries and clubs
library(rvest)
library(tidyverse)
library(tidygraph)
library(ggraph)
wc_squads <- read_html("https://en.wikipedia.org/wiki/2018_FIFA_World_Cup_squads") %>%
html_table()
teams <- c("Egypt", "Russia", "Saudi Arabia", "Uruguay",
"Iran", "Morocco", "Portugal", "Spain",
@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 / 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 / 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"
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 / party_points.R
Created April 2, 2018 11:37
How to map multivariate dot-density data with the R simple features package and ggplot2
library(tidyverse) # dev version of ggplot2 required devtools::install_github('hadley/ggplot2')
library(sf)
extrafont::loadfonts(device = "win")
# election results
ge_data <- read_csv("http://researchbriefings.files.parliament.uk/documents/CBP-7979/HoC-GE2017-constituency-results.csv") %>%
filter(region_name == "London") %>%
select(ons_id, constituency_name, con:green)