Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
View ggiraph-alt-text.R
library(ggplot2)
library(ggiraph)
data <- mtcars
data$carname <- row.names(data)
gg_point = ggplot(data = data) +
geom_point_interactive(aes(x = wt, y = qsec, color = disp,
tooltip = carname, data_id = carname)) +
View mercator-web-map-tile-conversion.R
# Conversion between Web Map Tiles and Mercator coordinates
require(tibble)
merc_to_wmt = function(x, y, z){
m_ext = pi * 6378137 # Mercator extent
res = 2 ^ (1:14)[z] # grid size
x_grid = scales::rescale(x, from = c(-m_ext, m_ext), to = c(0, res)) |> floor()
y_grid = scales::rescale(y, from = c(m_ext, -m_ext), to = c(0, res)) |> floor()
View tmap-pixel-bug.R
require(tmap)
require(dplyr)
require(stars)
require(shiny)
server = shinyServer(function(input, output, session) {
tmap_mode("view")
output$map = renderTmap({
View plotly-map-accuracy.html
This file has been truncated, but you can view the full file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style>body{background-color:white;}</style>
<script src="data:application/javascript;base64,KGZ1bmN0aW9uKCkgewogIC8vIElmIHdpbmRvdy5IVE1MV2lkZ2V0cyBpcyBhbHJlYWR5IGRlZmluZWQsIHRoZW4gdXNlIGl0OyBvdGhlcndpc2UgY3JlYXRlIGEKICAvLyBuZXcgb2JqZWN0LiBUaGlzIGFsbG93cyBwcmVjZWRpbmcgY29kZSB0byBzZXQgb3B0aW9ucyB0aGF0IGFmZmVjdCB0aGUKICAvLyBpbml0aWFsaXphdGlvbiBwcm9jZXNzICh0aG91Z2ggbm9uZSBjdXJyZW50bHkgZXhpc3QpLgogIHdpbmRvdy5IVE1MV2lkZ2V0cyA9IHdpbmRvdy5IVE1MV2lkZ2V0cyB8fCB7fTsKCiAgLy8gU2VlIGlmIHdlJ3JlIHJ1bm5pbmcgaW4gYSB2aWV3ZXIgcGFuZS4gSWYgbm90LCB3ZSdyZSBpbiBhIHdlYiBicm93c2VyLgogIHZhciB2aWV3ZXJNb2RlID0gd2luZG93LkhUTUxXaWRnZXRzLnZpZXdlck1vZGUgPQogICAgICAvXGJ2aWV3ZXJfcGFuZT0xXGIvLnRlc3Qod2luZG93LmxvY2F0aW9uKTsKCiAgLy8gU2VlIGlmIHdlJ3JlIHJ1bm5pbmcgaW4gU2hpbnkgbW9kZS4gSWYgbm90LCBpdCdzIGEgc3RhdGljIGRvY3VtZW50LgogIC8vIE5vdGUgdGhhdCBzdGF0aWMgd2lkZ2V0cyBjYW4gYXBwZWFyIGluIGJvdGggU2hpbnkgYW5kIHN0YXRpYyBtb2RlcywgYnV0CiAgLy8gb2J2aW91c2x5LCBTaGlueSB3aWRnZXRzIGNhbiBvbmx5IGFwcGVhci
View test-grid-data.csv
n lat lon
342 14.765625 45.703125
307 21.796875 94.921875
265 37.265625 127.265625
172 31.640625 35.859375
156 31.640625 34.453125
151 35.859375 37.265625
141 48.515625 38.671875
138 35.859375 35.859375
138 18.984375 -99.140625
View escape-triple-quoted.R
# function for parsing strings where quotes are not escaped but nested inside triple-quotes
escape_triple_quoted = function(j){
j_split = strsplit(j, '"{3}')[[1]]
f = seq_along(j_split) %% 2 == 0 # filter
j_split[f] = gsub('"', '\\\\"', j_split[f])
paste(j_split, collapse = '"')
}
# Usage
View fill-missing-timeseries.R
# identify and fill missing data points in timeseries data with regular-periodicity
require(padr)
#> Loading required package: padr
df <- data.frame(date = as.Date('2016-04-01') + 3*c(0,1,3,5,6), val = sample(5)) |>
print()
#> date val
#> 1 2016-04-01 3
#> 2 2016-04-04 1
View wayback-engine.R
require(wayback)
url = 'https://bbc.com'
wb = wayback::archive_available(url)
wb$closet_url
wb_mems = wayback::get_mementos(u)
wb_mems$link
View %IN%.R
# String in text (vectorised)
# A Python-style String in Text operator that ignores case. Either x or pattern must be a single value. Returns a logical value or vector.
{\(pattern, x) stringi::stri_detect_regex(x, pattern, case_insensitive = TRUE)} -> `%IN%`
# usage
'dog' %IN% 'the nice dog said hello'
#> [1] TRUE
View list-dependencies.R
# Function to report all explicit R dependencies in current directory (recursive)
# This ignores dependencies of dependencies, but output can be piped to tools::package_dependencies()
report_dependencies = function(path = './', print = FALSE, simple = TRUE){
files = list.files(path = path, pattern = '.R$', recursive = TRUE, full.names = TRUE)
scripts = lapply(files, readLines, warn = FALSE) |> unlist() |> paste(collapse = '\n')
loaded = scripts |> stringr::str_extract_all('(?<=(library|require)\\()[a-zA-Z0-9_.]+') |> unlist() |>
stringr::str_remove_all('["\']') |> stringr::str_remove(',.*') |> unique() |> sort()
invoked = scripts |> stringr::str_extract_all('[a-zA-Z0-9_.]+(?=::)') |> unlist() |> unique() |> sort()