Skip to content

Instantly share code, notes, and snippets.

View elipousson's full-sized avatar

Eli Pousson elipousson

View GitHub Profile
@elipousson
elipousson / make_worksheet_questions.R
Created November 24, 2024 16:42
Function to make basic worksheet questions
make_worksheet_questions <- function(range = c(0:25),
ops = c("+", "+", "+", "-", "-", "x"),
n = 30,
allow_neg = FALSE) {
lhs <- sample(range, n, replace = TRUE)
rhs <- sample(range, n, replace = TRUE)
ops <- sample(ops, n, replace = TRUE)
if (!allow_neg) {
for (i in seq(n)) {
library(microbenchmark)
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
# pak::pkg_install('geoarrow/[email protected]')
# https://github.com/geoarrow/geoarrow-r/issues/28
library(geoarrow)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
#&gt; Reading layer `nc' from data source 
library(tictoc)
library(sf)
# pak::pkg_install('geoarrow/[email protected]')
# https://github.com/geoarrow/geoarrow-r/issues/28
library(geoarrow)
tf_parquet <- tempfile(fileext = ".parquet")
tf_gpkg <- tempfile(fileext = ".gpkg")
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#' Safely use spdep::poly2nb and spdep::n.comp.nb to get an index of neighboring
#' features
#'
#' [spdep::poly2nb()] errors if no neighboring features exist. This wrapper
#' function returns a vector of the same length as x with the integer 0 for any
#' input object that typically errors. Adapted from code by Josiah Parry
#' inspired by work from CGMossa.
#'
#' @source <https://github.com/r-spatial/sf/issues/2422#issue-2459271220>
#' @inheritParams x A sf or sfc object
test_list <- list(
1,
c(1, 2),
c(2, 3),
c(3, 4),
4,
5
)
map(
@elipousson
elipousson / read_qmd_params.R
Created August 29, 2024 22:50
R function for reading a subset of front-matter keys for one or more Qmd documents into a table
#' Use lightparser::split_to_tbl to read YAML keys from one or more Quarto
#' document
#'
#' Created 2024-08-29 to reconcile the file names and schedule of the slides,
#' week overview pages, and exercises.
read_qmd_params <- function(path,
...,
recurse = FALSE,
keys = c("order",
"title",
@elipousson
elipousson / ymlthis-helpers.R
Created August 29, 2024 22:36
Helper functions using the ymlthis R package to read and update YAML front matter from a document
index_yml <- function(txt = NULL) {
if (rlang::is_string(txt) && fs::is_file(txt)) {
txt <- readLines(txt)
}
c(2:(which(txt == "---")[2] - 1))
}
read_yml <- function(path, remove = FALSE) {
txt <- readLines(path)
tar_url_read <- function(name,
command,
read,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
format = targets::tar_option_get("format"),
repository = targets::tar_option_get("repository"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
library(tidyverse)
salary_url <- "https://services1.arcgis.com/UWYHeuuJISiGmgXx/arcgis/rest/services/EmployeeSalaries_1/FeatureServer/0"
salary_service <- arcgislayers::arc_open(salary_url)
salary_table_src <- arcgislayers::arc_select(salary_service)
# pak::pkg_install("elipousson/baltimoredata")
salary_xwalk <- baltimoredata::entity_xwalk |>
library(d2r)
one_many <- c(
"Agency -> Service",
"Service -> Cost Center",
"Fund -> Cost Center",
"Award -> Grant",
"Revenue Category -> Budget Plan"
)