Skip to content

Instantly share code, notes, and snippets.

View mmparker's full-sized avatar

Matt Parker mmparker

View GitHub Profile
@mmparker
mmparker / batch_pandoc_win.bat
Created July 13, 2012 16:52
Batch conversion of .markdown to .html on Windows command line
REM This file converts all of the Markdown files to HTML.
REM Converting in the current directory
REM %%~ni returns just the filename of %%i, not its extension
for %%i in (*.markdown) do pandoc -f markdown -t html5 %%~ni.markdown > html/%%~ni.html
REM Converting a subdirectory - just slap it on front
for %%i in (report_pages/*.markdown) do pandoc -f markdown -t html5 report_pages/%%~ni.markdown > html/report_pages/%%~ni.html
library(dplyr)
df_list <- list(
mutate(mtcars[1:5, ], jurisdiction = 'New Guernsey'),
mutate(iris[1:5, ], mean = mean(Sepal.Length)),
mutate(beaver1[1:5, ], jurisdiction = 'New Guernsey', mean = mean(temp))
)
@mmparker
mmparker / printable_table.css
Created February 20, 2014 19:20
CSS for printing an HTML table, one row per page (it's a tall row)
@media print {
tr{
page-break-after: always;
display: block;
}
}
@mmparker
mmparker / calc_age.r
Last active August 13, 2021 18:58
Calculate years of age at a given date
# Calculate age at a given reference date
# Create an interval between the date of birth and the enrollment date;
# intervals are specific to the two dates. Periods give the actual length
# of time between those dates, so convert to period and extract the year.
calc_age <- function(birthDate, refDate = Sys.Date(), unit = "year") {
require(lubridate)
@mmparker
mmparker / named_color_vector.r
Last active May 14, 2021 10:44
An example of using a named color vector to set colors in ggplot2
# The incantation
options(stringsAsFactors = FALSE)
library(ggplot2)
# Some sample data
# A test that can result in a negative or positive only
testres2 <- data.frame(result = c("Negative", "Positive"),
library(dplyr)
library(rvest)
# Get the list of files to work on
files_to_process <- list.files('r:/shared documents/',
pattern = 'html',
full.names = TRUE)
@mmparker
mmparker / test_script.py
Created July 7, 2017 21:51
Trying to understand logging in Python with structlog...
# Two main things I can't figure out:
# 1. How do I get the name of each function to prefix its log entry?
# e.g., INFO:testmodule:outsideFun:{msg} instead of just INFO:testmodule:{msg}
# 2. How do I get bound values to persist through subsequent functions?
# e.g., user:matt.parker and outsideArg:foo should appear in both
# outsideFun and insideFun log messages
import logging
@mmparker
mmparker / nested_modules.R
Last active February 10, 2017 17:39
Trying to nest a plot-generating module inside a second module that creates several plots.
options(stringsAsFactors = FALSE,
scipen = 9999)
library(shiny)
library(ggplot2)
library(dplyr)
@mmparker
mmparker / if_else_bug.R
Last active November 2, 2016 16:43
When working with a sqlite tbl, if_else() won't accept named arguments - but *will* accept unnamed.
library(dplyr)
# Set up a temp sqlite database
db <- src_sqlite(tempfile(), create = TRUE)
iris2 <- copy_to(db, iris)
# if_else with named true and false on a data.frame -> no problem
iris %>% mutate(Sepal.Size = if_else(Sepal.Length > 5,
true = "big",
@mmparker
mmparker / multi_event_dates.R
Created October 4, 2016 22:06
An attempt to put date labels on an integer-sequenced x-axis, when there are multiple sequential points per date.
# Always
options(stringsAsFactors = FALSE)
library(tidyverse)
# Setting the ggplot2 theme