Skip to content

Instantly share code, notes, and snippets.

View matt-dray's full-sized avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
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")
@padpadpadpad
padpadpadpad / label_facets_ggplot2.R
Last active March 28, 2019 07:49
function to label facets with letters in ggplot2
# load package
library(ggplot2)
# write function
label_facets <- function(string){
len <- length(string)
string = paste('(', letters[1:len], ') ', string, sep = '')
return(string)
}
@Quiri
Quiri / server.R
Created September 9, 2014 10:05
Shiny disable checkbox minimal example
library(shiny)
shinyServer(function(input, output, session) {
observe({
if(input$tv == "All, but C") {
updateCheckboxGroupInput(session, "check2", label = NULL, choices = NULL,
selected = c("A", "B", "D", "E"))
}
if(input$tv == "Only C or D") {
@primaryobjects
primaryobjects / markov.R
Last active March 31, 2020 04:21
Generating text with a markov chain in 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)
@mrecos
mrecos / leaflet_flyTo_Shiny.r
Last active October 1, 2020 19:14
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
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)
@dublado
dublado / estimate read time calc hugo.md
Last active June 29, 2021 15:20
estimate read time calc hugo
<i>{{.ReadingTime}}{{if gt (mul .ReadingTime 60) 60}}minutes{{else}}minutes or less{{end}} reading</i>

formula
(add .WordCount 212) 213

.ReadingTime

@mpjdem
mpjdem / game_of_life.R
Last active October 7, 2021 16:08
Conway's game of life in R, visualised in the terminal
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]
@ivyleavedtoadflax
ivyleavedtoadflax / R_connect_to_MS_SQL_Server.md
Last active December 22, 2021 00:46
R Connect to MS SQL Server

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

@emitanaka
emitanaka / collapseoutput.js
Created July 20, 2019 04:43
Collapsible Code Output for `xaringan`
<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() {
@moodymudskipper
moodymudskipper / vassign.R
Last active June 13, 2022 21:54
vassign
makeActiveBinding("v", local({
e <- NULL
count <- 1
function(value) {
# increment or reinitialize counter
exec_env <- sys.frame(-1)
if(identical(e, exec_env)) {
count <<- count + 1
} else {