Skip to content

Instantly share code, notes, and snippets.

@jeffeaton
jeffeaton / gist:f804da95e109e2e7242642a4d927514f
Created November 19, 2016 18:57
read a stata dataset into R from a zip file
read_zipdta <- function(zfile){
tmp <- tempfile()
on.exit(unlink(tmp))
return(foreign::read.dta(unzip(zfile, grep(".dta", unzip(zfile, list=TRUE)$Name, TRUE, value=TRUE), exdir=tmp)))
}
@jeffeaton
jeffeaton / report.R
Last active January 23, 2020 03:28
Simple YAML header for reports with `knitr::spin()`
#' ---
#' title: "<title>"
#' author: Jeff Eaton
#' output: pdf_document
#' ---
#'
##+ setup, include=FALSE
library(knitr)
opts_chunk$set(tidy=TRUE, warning=FALSE, cache=TRUE, message=FALSE)
@jeffeaton
jeffeaton / dide-deploy-shiny.R
Created July 7, 2018 15:08
Deploying app on DIDE Shiny server
devtools::install_github("mrc-ide/hivmappr@shiny")
## 1. Bundle app using `rsconnect` internals.
appDir <- "~/Documents/Code/R/hivmappr/inst/shiny/hivmappr/"
appPath <- appDir
target <- rsconnect:::deploymentTarget(appPath,
appName = NULL,
@jeffeaton
jeffeaton / scam-example.R
Created February 27, 2019 12:10
working example of shape constrained additive models from scam package in Stan
library(scam)
library(rstan)
library(ggplot2)
set.seed(0)
n <- 200
x1 <- runif(n)*6-3
f1 <- 3*exp(-x1^2) # unconstrained term
f1 <- (f1-min(f1))/(max(f1)-min(f1)) # function scaled to have range [0,1]
x2 <- runif(n)*4-1;
@jeffeaton
jeffeaton / shiny90-datapack-extract.R
Created February 6, 2020 06:33
Function to export proportion aware of status by age and sex from .shiny90 output file
#' Export proportion aware by five year age group from Shiny90
#'
#' Export estimates for proportion aware of status from a Shiny90
#' output file to five-year age groups 15-19 to 50+.
#'
#' @param shiny90_path file path to .shiny90 digest file.
#' @param out_path output path to save CSV of proportion aware.
#' @param year year(s) to generate estimates; an integer or a vector of integers.
#'
#' @return
@jeffeaton
jeffeaton / shiny90-datapack-extract.R
Last active February 12, 2020 12:36
Function to export proportion aware of status by age and sex from .shiny90 output file
#' Export proportion aware by five year age group from Shiny90
#'
#' Export estimates for proportion aware of status from a Shiny90
#' output file to five-year age groups 15-19 to 50+.
#'
#' @param shiny90_path file path to .shiny90 digest file.
#' @param out_path output path to save CSV of proportion aware.
#' @param year year(s) to generate estimates; an integer or a vector of integers.
#'
#' @return
#' This script is an example of the function subset_output_package() to load,
#' subset, and resave the Naomi output package.
#'
#' The arguments to the function are:
#'
#' * path: file path to the output package.
#' * output_path: file path to save the new subsetted package (end in .zip).
#' * area_id: vector of area_ids to keep or drop.
#' * area_level: vector of area_levels to keep or drop.
#' * sex: vector of sexes to keep or drop.
@jeffeaton
jeffeaton / malawi-census-2018-district-population-projections.R
Last active December 8, 2020 09:50
Malawi Census 2018 District population projections (2018-2043)
library(dplyr)
library(pdftools)
library(readr)
library(stringr)
library(tidyr)
url <- "http://www.nsomalawi.mw/images/stories/data_on_line/demography/census_2018/Thematic_Reports/Population%20Projections%202018-2050.pdf"
file <- tempfile(fileext = ".pdf")
download.file(url, file)
@jeffeaton
jeffeaton / adjust_datapack_export2021.R
Last active March 8, 2021 09:26
Adjust PEPFAR Data Pack export for 2021 specification
#' This function saves an updated data pack export CSV in the same directory as
#' the Naomi output package file
#'
#' Example:
#'
#' file <- "~/Downloads/MWI_20210308-090536_naomi_spectrum_digest.zip" ## !! REPLACE HERE
#' adjust_datapack_export2021(file)
#'
adjust_datapack_export2021 <- function(naomi_output_package) {
@jeffeaton
jeffeaton / epp-xml-anc-update.R
Last active December 16, 2022 05:04
Function to update ANC data in EPP XML file within Spectrum PJNZ
library(xml2)
library(epp)
eppd_overwrite_matrix <- function (xm, mat) {
if (!xml_attr(xm, "class") %in% c("[D", "[I"))
stop("Tried to invoke .parse_matrix() on array node not of class '[D' or '[I'.")
m_rows <- as.integer(xml_attr(xm, "length"))
stopifnot(nrow(mat) == m_rows)