Skip to content

Instantly share code, notes, and snippets.

View bjcairns's full-sized avatar

Benjamin Cairns bjcairns

View GitHub Profile
@bjcairns
bjcairns / stop.Rmd
Created April 16, 2019 13:29
Add a button to a learnr tutorial (or other pre-rendered Shiny document) to stop Shiny
---
title: "Stop learnr tutorial with a button"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
```
@bjcairns
bjcairns / personal-package.R
Last active December 3, 2019 12:14
Script to set up a personal package
# Create a new RStudio package project in a directory of your choice
# We'll assume it's called, "mypkg"
# Check "Create a git repository" if you use git!
# Packages we'll need
install.packages(c(
"devtools",
"usethis",
"roxygen2",
"testthat"
@bjcairns
bjcairns / argument-handling.R
Last active January 17, 2020 13:57
Handling argument priorities (specified vs argument list vs default) in R
# Sometimes during repeat calls to a function, it may be helpful if arguments used in a
# previous call are passed as a list to the function in subsequent calls, to be
# superseded if individual arguments of the same name are given.
#
# The following function prioritises specified arguments (including ... arguments) over an
# argument list, and the argument list over default values.
handle_args <- function(arg1 = 1, arg2 = 2, arglist = list(), ...) {
# Copy default arguments
@bjcairns
bjcairns / covid_eng_fit.R
Created September 29, 2020 23:28
Download the COVID-19 case counts for England and fit a simple smoothed model accounting for day-of-the-week
library(dplyr)
library(mgcv)
library(ggplot2)
# Download
c19 <- readr::read_csv("https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv")
# Extract case counts for England and select useful variables
c19_Eng <- c19 %>% filter(`Area type` == "nation") %>%
transmute(date = `Specimen date`, cases = `Daily lab-confirmed cases`)