Skip to content

Instantly share code, notes, and snippets.

View edonnachie's full-sized avatar

Ewan Donnachie edonnachie

View GitHub Profile
@edonnachie
edonnachie / Makefile_R_Windows.mk
Created July 25, 2018 12:12
Pattern rules for using GNU make to build R projects on Windows
################################################################################
## Automatic rules ----
################################################################################
# First problem on windows: where to find Rscript if not on path
# RSCRIPT = "C:/Program Files/R/R-3.4.2/bin/x64/Rscript" --verbose
RSCRIPT = "Rscript" --verbose
# R files with Rout as target
@edonnachie
edonnachie / read_chunk_Rmd.R
Created November 22, 2021 11:59
Make chunks from one .Rmd document available to a different .Rmd document
read_chunk_Rmd <- function(path, ...) {
f_tmp <- tempfile()
knitr::purl(input = path, output = f_tmp)
on.exit(file.remove(f_tmp))
knitr::read_chunk(f_tmp, ...)
}
@edonnachie
edonnachie / sf_map_overlay.R
Last active May 9, 2022 21:57
Demonstration of how to overlay one region onto another using R and sf
library(sf)
library(ggplot2)
# Data from https://www.geoboundaries.org/index.html#getdata
de2 <- read_sf("data/geoBoundaries-DEU-ADM2-all/geoBoundaries-DEU-ADM2_simplified.shp")
# Extract Munich and Berlin to separate objects
munich <- dplyr::filter(de2, DistrictCo == "09162")
berlin <- dplyr::filter(de2, DistrictCo == "11000")
library(dplyr)
library(arrow)
library(duckdb)
# install.packages(c("dplyr", "arrpw", "duckdb", "nycflights13"))
## Export the nycflights13 dataset to arrow format ----
nycflights13::flights |>
@edonnachie
edonnachie / render_preview.R
Last active December 27, 2022 14:30
Call rmarkdown::render interactively to create a preview version of a Rmd
#' Call rmarkdown::render to create a preview version of a Rmd
#'
#' This function is intended to be used interactively to create a preview
#' version of a Rmd (e.g. compile a chapter of a bookdown project to docx).
#' By default, it renders the file currently opened in the RStudio source
#' editor, and saves the output to a subfolder called "preview".
#'
#' The default format is html. The argument word = TRUE will change the output
#' format to docx, and using pdf = TRUE will create PDF output.
#'