Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
library(tidyverse)
set.seed(7)
N = 1000
x = rnorm(N)
p = plogis(0.2*x - 0.8)
y = rbinom(N, 1, p)
tibble(x, y) %>%
mutate(z = cut_number(x, 5)) %>%
// debug javscript errors with alerts
window.onerror = function (msg, url, lineNo, columnNo, error) {
var error_msg = ['msg: ' + msg, 'url: ' + url, 'lineNo: ' + lineNo, 'columnNo: ' + columnNo, 'error: ' + error].join('\n');
window.alert(error_msg);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script> -->
</head>
<body>
<div id="game-area"></div>
<h1 style="left: 20px; top: 20px; position:absolute;">Mouse</h1>
// stringify an even object for inspection
function stringifyEvent(e) {
const obj = {};
for (let k in e) {
obj[k] = e[k];
}
return JSON.stringify(obj, (k, v) => {
if (v instanceof Node) return 'Node';
@geotheory
geotheory / sitemap.R
Created February 19, 2021 22:24
R sitemap generator
# source: https://github.com/jeroen/curl/blob/master/examples/sitemap.R
#
### R sitemap example, Jeroen Ooms, 2016
#
# This code demonstrates the new multi-request features in curl 2.0. It creates
# an index of all files on a web server with a given prefix by recursively following
# hyperlinks that appear in HTML pages.
#
# For each URL, we first perform a HTTP HEAD (via curlopt_nobody) to retrieve
# the content-type header of the URL. If the server returns 'text/html', then we
require(raster, quietly=T)
require(dplyr, quietly=T, warn.conflicts=F)
download.file('https://data.worldpop.org/GIS/Population/Global_2000_2020/2020/LBN/lbn_ppp_2020_UNadj.tif',
destfile = 'lebanon-pops.tif')
download.file('https://data.worldpop.org/GIS/Pixel_area/Global_2000_2020/LBN/lbn_px_area_100m.tif',
destfile = 'lebanon-areas.tif')
pop_grid <- raster('lebanon-pops.tif')
area_grid <- raster('lebanon-areas.tif') / 1000000 # square meters to kms
# dummy dataset
require(gutenbergr)
d = ggplot2::diamonds
d$txt = gutenberg_download(1184)$text[1:nrow(d)]
filename = "~/Downloads/dat.csv"
readr::write_csv(d, filename)
#-------------------
@geotheory
geotheory / pandoc.css
Created January 7, 2021 14:45 — forked from killercup/pandoc.css
Add this to your Pandoc HTML documents using `--css pandoc.css` to make them look more awesome. (Tested with Markdown and LaTeX.)
/*
* I add this to html files generated with pandoc.
*/
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
# test if a file is a valid zip archive
# can be used to distinguish old MS documents from current xml formats (e.g. doc from docx)
valid_zip = function(filepath, zippath = "/usr/bin/zip"){
zip_status = suppressWarnings(system(paste0(zippath, ' -T "', filepath, '"'), intern = TRUE))
return(length(stringr::str_subset(zip_status, 'OK$')) > 0)
}
# e.g.
# valid_zip("file.doc")