Skip to content

Instantly share code, notes, and snippets.

View JosiahParry's full-sized avatar
💻
on the 'puter

Josiah Parry JosiahParry

💻
on the 'puter
View GitHub Profile
@JosiahParry
JosiahParry / gist:539f343bc8b17ed1396fcee5ae5cc346
Created January 18, 2025 19:53
API done 2 wayys ambiorix vs plumber
app <- ambiorix::Ambiorix$new()
app$post("/process", function(req, res) {
body <- yyjsonr::read_json_raw(req$rook.input$read())
msg <- sprintf(
"Hello, %s! Your age is %i, and your email is %s.",
body[["name"]], body[["age"]], body[["email"]]
)
res$json(list(message = msg, status = "success"))
})
@JosiahParry
JosiahParry / callback-log.R
Created January 7, 2025 22:52
callback log function?
log <- tempfile(fileext = ".log")
con <- file(log, open = "a")
addTaskCallback(
function(expr, value, ok, visible) {
if (rlang::is_condition(value)) {
write("Found a condition!!!!", con, append = TRUE)
}
if (visible) {
msg <- sprintf("[%sZ] > %s", format(Sys.time()), format(expr))
@JosiahParry
JosiahParry / auto-spat-reg.R
Created January 6, 2025 19:23
Drafting automatic spatial reg sfdep/spdep
library(sfdep)
library(dplyr)
library(spdep)
guerry_nb |>
reframe(across(where(is.numeric), \(.x) broom::tidy(global_moran_perm(.x, nb, wt)))) |>
tidyr::pivot_longer(everything()) |>
tidyr::unnest(value)
moran_all <- function(.data, vars, nb_col = "nb", wt_col = "wt") {
@JosiahParry
JosiahParry / renv.rs
Last active January 4, 2025 20:52
{renv} reader and writer in Rust
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RenvLock {
pub r: RenvRVersion,
@JosiahParry
JosiahParry / read-swm.R
Last active December 31, 2024 17:00
Read an ArcGIS SWM File as spdep listw
# Original function
read_swm <- function(file) {
con <- file(file, "rb")
header <- NULL
while(TRUE) {
r <- readBin(con, "raw", size=1L, n=1, endian="little")
if (r == charToRaw("\n")) break
else header <- c(header, r)
}
cheader <- rawToChar(header)
@JosiahParry
JosiahParry / focal-mean.R
Last active November 28, 2024 18:44
focal mean using base R is faster than using terra
library(terra)
library(Matrix)
# create a sample raster
r <- rast(ncols=10, nrows=10, ext(0, 10, 0, 10))
x <- 1:ncell(r)
values(r) <- x
# why is there NaN in here????
adj_raw <- adjacent(r, 1:ncell(r), "queen")
cumsum_cut <- function(x, cuts) {
  x[cuts-1] <- 0
  res <- lapply(split(x, cumsum(x == 0)), \(.x) {
    cumsum(.x)
  }) |> 
    unlist() |>
    unname()
  n <- length(res)
 to_fill &lt;- numeric(n)
@JosiahParry
JosiahParry / game-of-life-spdep.R
Created October 16, 2024 15:56
The game of life written in R with spdep
library(spdep)
n <- 25
geo <- sf::st_make_grid(
cellsize = c(1, 1),
offset = c(0, 0),
n = n
)
# create contiguity neighbors for the grid
str_replace_many <- function(string, ...) {
replacements <- rlang::list2(...)
patterns <- rlang::names2(replacements)
if (anyNA(patterns) || any(!nzchar(patterns))) {
cli::cli_abort("All arguments passed to {.arg ...} must be named")
}
for (pattern in replacements) {
rlang:::check_string(pattern, arg = pattern)
}
@JosiahParry
JosiahParry / Cargo.toml
Last active September 30, 2024 23:12
Blackwell's N=2
[package]
name = "my-hands"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
rand_distr = "0.4.3"