Skip to content

Instantly share code, notes, and snippets.

@PaulC91
PaulC91 / addBubbleLegend.R
Last active July 3, 2019 13:11
function for adding a scaled circle legend to a leaflet map in R
addBubbleLegend <- function(
map, title = "", range, scaling_fun, ...,
strokeColor, weight, fillColor, fillOpacity,
position = c("topright", "bottomright", "bottomleft", "topleft"),
data = getMapData(map), layerId = NULL
) {
range <- base::pretty(sort(range), 20)
range <- range[range != 0]
min_n <- min(range, na.rm = TRUE)
med_n <- median(range, na.rm = TRUE)
@PaulC91
PaulC91 / waffle_bars.R
Last active May 6, 2019 13:46
make waffle bar charts (tiled bar charts?) in ggplot2 with numeric y-axis labels
remotes::install_github("paulc91/waffle", ref = "1.0.0")
library(tidyverse)
library(waffle)
storms %>%
filter(year >= 2010) %>%
count(year, status) -> storm_df
ggplot(storm_df, aes(fill=status, values=n)) +
@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)
)
@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 / 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 / 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 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 / 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 / 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)