Skip to content

Instantly share code, notes, and snippets.

library(data.table)
library(magrittr)
library(grattan)
library(SampleFile1415)
library(taxstats)
library(hutils)
library(scales)
tax2014 <- copy(sample_file_1415) %>% apply_super_caps_and_div293()
@HughParsonage
HughParsonage / CGT_tax_expenditure_1415
Created January 18, 2018 05:39
CGT tax expenditure
library(magrittr)
library(grattan)
library(data.table)
library(SampleFile1415)
as.data.table(sample_file_1415) %>%
.[, tax_noCGT := Taxable_Income + Net_CG_amt] %>%
.[, tax := income_tax(Taxable_Income, "2014-15", .dots.ATO = sample_file_1415)] %>%
.[, tax_new := income_tax(tax_noCGT, "2014-15", .dots.ATO = sample_file_1415)] %>%
.[, .(tot = round(50 * sum(tax_new - tax) / 1e9, 1))]
#' Quickly count the number of files in a directory
number_of_files <- function(dir, filetype = "*.*") {
if (.Platform$OS.type == "windows" &&
nzchar(Sys.which("powershell"))) {
current_wd <- getwd()
on.exit(setwd(current_wd))
setwd(dir)
result <- system2("powershell",
@HughParsonage
HughParsonage / ghostscript-frontpage-compress.R
Created November 5, 2017 00:39
For compressing frontpages
paste("gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer",
"-dNOPAUSE -dQUIET -dBATCH", "-sOutputFile=output.pdf",
"report-cover-energy-nem-latex.pdf")
parse2 <- function(x) {
# x <- "ab{c{f}}d{e}f{g}h{ij{{k}}}lm"
x_split <- unlist(strsplit(x, "", fixed = TRUE))
tex_group <- cumsum(x_split == "{") - cumsum(x_split == "}")
max_tex_group <- max(tex_group)
out <- setDT(list(char = x_split, tex_group = tex_group))
out[, I := .I]
@HughParsonage
HughParsonage / employment-QLD
Last active August 27, 2017 14:05
SA4 employment figures by QLD region
library(grattanCharts) # devtools::install_github('hughparsonage/grattanCharts')
library(dplyr)
library(httr)
library(tidyr)
library(readxl)
library(scales)
library(ggplot2)
library(data.table)
library(lubridate)
library(hutils)
@HughParsonage
HughParsonage / HILDA-household-disp-Gini.R
Created July 25, 2017 14:30
Use HILDA to calculate Gini by household
library(data.table)
library(hildaData)
library(magrittr)
library(grattanCharts)
library(ggplot2)
#' @source acid package (but it's chock full of unnecessary dependencies)
weighted_gini <- function (x, w = NULL) {
if (is.null(w))
w <- rep(1, length(x))
formalCoverage <- function(fn) {
formalz <<- formals(fn)
bod <<- as.character(body(fn))
formal_present <- function(formal) {
formal_dots_escaped <<- gsub(".", "\\.", formal, fixed = TRUE)
any(grepl(pattern = paste0("(?:\\b|\\s)", formal_dots_escaped, "(?:\\b|\\s)"),
x = bod,
perl = TRUE))
@HughParsonage
HughParsonage / weather_here.R
Created June 25, 2017 01:03
Prints the weather at your location (based on IP)
if (requireNamespace("bomrang", quietly = TRUE)) {
library(data.table)
library(bomrang) # devtools::install_github('ToowoombaTrio/bomrang')
`_nrst_stn_to_me` <- as.numeric(jsonlite::fromJSON("http://ip-api.com/json")[c("lat", "lon")])
weather_here <- function() {
bomrang::get_current_weather(latlon = `_nrst_stn_to_me`, as.data.table = TRUE) %>%
.[, .(local_date_time, weather, rel_hum, apparent_t, air_temp, wind_dir, wind_spd_kt, gust_kt)] %>%
head
}
@HughParsonage
HughParsonage / HILDA-household-stats-for-hhs-retiree-super-exceeds-1,600,000.R
Last active May 9, 2017 11:17
HILDA-household-stats-for-hhs-retiree-super-exceeds-1.6-million
library(testthat)
library(dplyr)
library(dtplyr)
library(data.table)
library(hildaData)
library(hildaExtra)
library(sih.hilda.1314)
library(taxstats)
library(magrittr)