Skip to content

Instantly share code, notes, and snippets.

View MilesMcBain's full-sized avatar

Miles McBain MilesMcBain

  • Queensland Fire and Emergency Services
  • Brisbane, Queensland
View GitHub Profile
@MilesMcBain
MilesMcBain / plan.R
Created April 10, 2020 11:17
Example drake plan
the_plan <-
drake_plan(
start_date = as_datetime("2018-01-01"),
end_date = as_datetime("2020-01-01") - seconds(1),
permanent_limit = 12 * 60,
auxiliary_limit = 7 * 60,
percentile = 0.9,
@MilesMcBain
MilesMcBain / nogorups.R
Created November 19, 2019 12:17
destroyer of groups
library(dplyr)
library(nycflights13)
mutate <- function(..dat, ...) {
ungroup(
dplyr::mutate(..dat, ...)
)
}
mutate... <- function(..dat, ...) {
@MilesMcBain
MilesMcBain / lon_lat_to_point.R
Last active April 30, 2020 00:54
lon_lat_to_point
##' Create an sf POINT column from long and lat columns
##'
##' This is a vectorised function that takes 2 columns of longitude and latitude
##' as arguments and returns a simple features geometry collection column in the
##' EPSG 4326 coordinate reference system (lon, lat).
##'
##' @title lon_lat_to_point
##' @param lon a numeric column of longitudes
##' @param lat a numeric column of latitudes
##' @return a simple features collection column of POINT geometries representing the lon/lat pairs.
@MilesMcBain
MilesMcBain / date_facets.R
Created October 14, 2019 12:14
date_facets
library(tidyverse)
library(lubridate)
epoch <- ymd("20080101")
date_frame <-
data.frame(
date = days(seq(1, 365 * 10, 1)) + epoch,
response = cumsum(rnorm(365 * 10))
)
callr::r(function(){
renv::init(bare = TRUE)
})
callr::r(function(){
renv::deactivate()
})
renv::hydrate(renv::dependencies("./packages.R")$Package,
library = renv::paths$library())
@MilesMcBain
MilesMcBain / renv.md
Last active August 9, 2019 07:50
Renv notes:

Set up library

  1. renv::init(bare = TRUE)

  2. renv::deactivate()

  3. renv::install(renv::dependencies("./packages.R")$Package)

Errors on GitHub only packages.

```` r
target_text <- "foo = 1 ``` ```{r} foo = 2"
tstfile = srcfile(tempfile())
parse(text = target_text,
keep.source = TRUE,
srcfile = tstfile)
#> Error in parse(text = target_text, keep.source = TRUE, srcfile = tstfile): attempt to use zero-length variable name
getParseData(tstfile)
#> NULL
@MilesMcBain
MilesMcBain / filter_fail.R
Created July 5, 2019 02:45
.data filter fail
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
iris %>%
@MilesMcBain
MilesMcBain / fools_five.R
Last active July 17, 2019 03:11
Fool's five
f <- function(...) {
function(df) with(df, ...)
}
footate <- function(.data, ...) {
dots <- list(...)
for (column in names(dots)) {
.data[[column]] <- dots[[column]](.data)
}
@MilesMcBain
MilesMcBain / sep_cols.R
Created June 20, 2019 03:57
regex separate
library(tidyverse)
tibble::tribble(
~name,
"John SmithManager",
'Miles McBain'
) %>%
tidyr::separate(name, into = c("name", "pos"),
sep = "(?<=[a-z]{2})(?=[A-Z])" )