Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / wc_winners_animation.R
Last active November 5, 2021 01:24
R code to produce an animated map of FIFA World Cup Winners/Runners-up using the new gganimate package
library(tidyverse)
library(rvest)
library(opencage) # need to get opencage API key and save as env variable below https://geocoder.opencagedata.com/pricing
library(sf)
library(rnaturalearth)
library(gganimate) # devtools::install_github('thomasp85/gganimate')
library(hrbrthemes) # devtools::install_github('hrbrmstr/hrbrthemes')
Sys.setenv(OPENCAGE_KEY = "xxxxxxxxxxxxxxxx")
@PaulC91
PaulC91 / app.R
Last active March 3, 2020 05:25
Mobile friendly shiny app map layout with collapsible input panel, for Roke.
library(shiny)
library(shinyjs)
library(leaflet)
ui <- bootstrapPage(
tags$head(includeCSS("styles.css")),
shinyjs::useShinyjs(),
@PaulC91
PaulC91 / app.R
Created November 30, 2018 15:36
return key trigger actionButton click in shiny example
library(shiny)
ui <- fluidPage(
tags$head(includeScript("returnClick.js")),
textInput("myText", "", placeholder = "Enter text then hit return", width = "100%"),
actionButton("myButton", "Go!"),
verbatimTextOutput("textOutput")
@PaulC91
PaulC91 / app.R
Last active April 24, 2023 18:47
Example of how to use shinyauthr with a shiny navbarPage UI
library(shiny)
library(shinyauthr)
library(shinyjs)
user_base <- data.frame(
user = c("user1", "user2"),
password = c("pass1", "pass2"),
permissions = c("admin", "standard"),
name = c("User One", "User Two"),
stringsAsFactors = FALSE
@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)
)
@PaulC91
PaulC91 / packrat_init.R
Created April 9, 2019 08:02
lightweight packrat setup as outlined here: https://milesmcbain.xyz/packrat-lite/
# =================================================================================
# lightweight packrat setup as outlined here: https://milesmcbain.xyz/packrat-lite/
# =================================================================================
# initiliase a packrat project in your project directory
packrat::init(
infer.dependencies = FALSE,
options = list(vcs.ignore.lib = TRUE, vcs.ignore.src = TRUE)
)