Skip to content

Instantly share code, notes, and snippets.

@PaulC91
PaulC91 / sf_to_duckdb.R
Created February 18, 2024 10:16
sf to duckdb via gpkg and back again
library(sf)
library(DBI)
library(duckdb)
# sf object
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
nc_crs <- st_crs(nc)
# write it to local gpkg
path_gpkg <- here::here("local.gpkg")
@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 / addCircleLegend.R
Created March 31, 2020 11:18
Add a custom circle legend to leaflet map in R
#' Add custom circle legend to leaflet map
#'
#' To be used alongside \code{leaflet::addCircleMarkers}
#'
#' @param map a leaflet map
#' @param title title of the legend
#' @param range vector of numeric values you want to scale the legend to (same vector used with addCircleMarkers)
#' @param scaling_fun the scaling function used with addCircleMarkers to scale circle radii appropriately for leaflet
#' @param color stroke color
#' @param weight stroke width in pixels
@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 / shiny_ppt_builder.R
Last active April 22, 2023 05:28
Example of using shiny to allow a user to build several plots interactively and iteratively add them as slides of a ppt deck download
library(shiny)
library(magrittr)
library(ggplot2)
library(officer)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
actionButton("add_plot", "Add to ppt"),
library(ggplot2)

scale_fill_msf <- function(...) {
  ggplot2:::manual_scale(
    "fill",
    values = c(
      "MSF" = "#D02327",
      "OCA" = "#EF5423",
 "OCB" = "#FFC70A",
@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)
@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 / launch_module.R
Last active November 25, 2021 13:32
Example of code to only launch a shiny module when its tab is selected for the first time
tab2_init <- TRUE
observeEvent(input$tabs, { # input$tabs is the menu input
if (all(input$tabs == "tab2", tab2_init)) {
# change to false so it won't run again
tab2_init <<- FALSE
# call server module
tab2Server("id", ...)
}
})
@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")