Skip to content

Instantly share code, notes, and snippets.

Avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@mrecos
mrecos / leaflet_flyTo_Shiny.r
Last active Oct 1, 2020
A (mostly) minimal example of using sidebar drop downs to 1) filter counties within states, and 2) `flyTo` the centroid of the selected state in shiny + leaflet
View leaflet_flyTo_Shiny.r
library(shiny)
library(shinydashboard)
# devtools::install_github("nik01010/dashboardthemes")
library(dashboardthemes)
library(tidyverse)
library(sf)
library(leaflet)
library(usmap) # us counties and states as table
library(leaflet)
@mpjdem
mpjdem / game_of_life.R
Last active Oct 7, 2021
Conway's game of life in R, visualised in the terminal
View game_of_life.R
library(data.table)
library(keypress)
## Create the universe. Well, sort of.
dims <- c(49, 49)
universe <- CJ(x = seq(dims[1]), y = seq(dims[2]), k = 1, cell = FALSE)
universe[, cell := sample(c(FALSE, TRUE), prod(dims), TRUE)]
## Neighbourhood to consider for each cell
neighbours <- CJ(xd = -1:1, yd = -1:1, k = 1)[xd != 0 | yd != 0]
@batpigandme
batpigandme / belchers_listcol.R
Last active Dec 16, 2020
Example of using `tidyr::hoist()`.
View belchers_listcol.R
library(tidyverse)
family <- list(
list(
"name" = "Bob",
"age" = 46,
"mother" = NA,
"father" = "Big Bob",
"siblings" = list(NA),
"children" = list("Tina", "Gene", "Louise"),
@emitanaka
emitanaka / collapseoutput.js
Created Jul 20, 2019
Collapsible Code Output for `xaringan`
View collapseoutput.js
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Output</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {
@padpadpadpad
padpadpadpad / label_facets_ggplot2.R
Last active Mar 28, 2019
function to label facets with letters in ggplot2
View label_facets_ggplot2.R
# load package
library(ggplot2)
# write function
label_facets <- function(string){
len <- length(string)
string = paste('(', letters[1:len], ') ', string, sep = '')
return(string)
}
@dublado
dublado / estimate read time calc hugo.md
Last active Jun 29, 2021
estimate read time calc hugo
View estimate read time calc hugo.md
<i>{{.ReadingTime}}{{if gt (mul .ReadingTime 60) 60}}minutes{{else}}minutes or less{{end}} reading</i>

formula
(add .WordCount 212) 213

.ReadingTime

View df a list
library(tidyverse)
files <- list.files("../open-data/", pattern = "^2017", full.names = TRUE)
full <- map_df(files, read_csv)
dplyr::glimpse(full)
# With names
files <- list.files("../open-data/", pattern = "^2017", full.names = TRUE) %>%
set_names(basename(.))
full <- map_df(files, read_csv, .id = "file")
@niw
niw / fetch_nike_puls_all_activities.bash
Last active Apr 30, 2022
A simple NikePlus API description to fetch past run metrics
View fetch_nike_puls_all_activities.bash
#!/usr/bin/env bash
# fetch_nike_puls_all_activities.bash
# A simple bash script to fetch all activities and metrics from NikePlus.
# See `nike_plus_api.md` for the API details.
readonly bearer_token="$1"
if [[ -z "$bearer_token" ]]; then
echo "Usage: $0 bearer_token"
exit
View R_connect_to_MS_SQL_Server.md

Connecting to MS SQL Server using RODBC

You may need to install the following libraries (if you don't already have them)

install.packages("RODBC")
install.packages("dplyr")

Load these packages

@primaryobjects
primaryobjects / markov.R
Last active Mar 31, 2020
Generating text with a markov chain in R.
View markov.R
library(markovchain)
text <- readLines('text.txt')
text <- text[nchar(text) > 0]
text <- gsub('.', ' .', text, fixed = TRUE)
text <- gsub(',', ' ,', text, fixed = TRUE)
text <- gsub('!', ' !', text, fixed = TRUE)
text <- gsub('(', '( ', text, fixed = TRUE)
text <- gsub(')', ' )', text, fixed = TRUE)