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 / 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)
@benzipperer
benzipperer / function_names.R
Created October 19, 2024 01:42
show all the function names in a given set of R scripts using the treesitter package
# show all the function names in a given set of R scripts
function_definitions = function(files) {
purrr:::map(files, function_definitions_script) |>
purrr::list_rbind()
}
function_definitions_script = function(file) {
text = brio::read_lines(file) |>
paste(collapse = "\n")
library(httr2)
library(purrr)
endpoint <- "https://models.inference.ai.azure.com"
token <- Sys.getenv("github_token")
model <- "gpt-4o-mini"
system <- list(
"role" = "system",
"content" = "You are a helpful assistant."
@lionel-
lionel- / tidyverse-c-api-compat.md
Last active September 9, 2025 08:48
Compatibility of tidyverse with the public C API of R

Summary of meeting between Tidyverse members and Luke Tierney at useR! 2024.

Frame/Environment inspection

Frontends and low level tools need to know what kind of bindings they are dealing with. Objectives include:

  • Avoiding side effects such as triggering a promise or causing a missing argument error. Low level tools often can't afford to protect against those for every variable lookup. Figuring out what happened by inspecting errors is also ambiguous, and sometimes impossible (promises may cause longjumps in a variety of ways).

  • Transparency in debugging/development settings. Providing context to the user about what's going to happen if they attempt to retrieve the value of a binding (i.e. an active binding invokation, a promise forcing leading to the evaluation of such and such expression, etc).

@boyswan
boyswan / tw.rs
Created June 28, 2024 14:04
Tailwind logic macro
#[macro_export]
macro_rules! tw {
// Base case: when there are no tokens left, return an empty string.
() => (String::new());
// New case: handle static string literals
($static:literal ; $($rest:tt)*) => {{
let mut class = String::from($static);
let rest_classes = tw!($($rest)*);
if !rest_classes.is_empty() {
@jrosell
jrosell / shiny-javascript-communication.R
Last active April 5, 2024 12:44
Update value in a hidden field from R or from JavaScript using the Shiny package.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
hidden(
textInput("my_input", label = "my_input", value = "Initial value")
),
textOutput("my_output"),
actionButton("my_js_button","Update from JavaScript"),
@JosiahParry
JosiahParry / vacc-simd.rs
Created April 2, 2024 16:16
Recreating the vacc function with simd from advanced R. This isn't very well done. It only returns in multiples of 4. This is probably because of `array_chunks::<4>()`. Its surprisingly slower than anticipated?
#![feature(portable_simd)]
#![feature(array_chunks)]
#[extendr]
fn vacc(age: &[f64], female: &[u8], ily: &[f64]) -> Vec<f64> {
age.array_chunks::<4>()
.map(|&a| f64x4::from_array(a))
.zip(female.array_chunks::<4>().map(|&f| u8x4::from_array(f)))
.zip(ily.array_chunks::<4>().map(|&i| f64x4::from_array(i)))
.map(|((a, f), i)| {
@strengejacke
strengejacke / .lintr
Last active October 31, 2025 04:01
VS Code setup for R
// save to windows-user directory
linters: with_defaults(object_name_linter = NULL,
object_length_linter(50),
commented_code_linter = NULL,
object_usage_linter = NULL,
line_length_linter(120),
cyclocomp_linter = cyclocomp_linter(50))
@EmilHvitfeldt
EmilHvitfeldt / new_render.R
Created December 12, 2020 19:39
Render files in subdirectories for markdown websites
new_render <- function(dir = "readings") {
old_names <- fs::dir_ls(dir)
new_names <- basename(old_names)
if (any(new_names %in% fs::dir_ls())) {
stop("Files in folders can not have same name as main files")
}
require("sf")
require("dplyr")
require("hexbin")
# Linux libertine font "sf", converted to path with Inkscape,
# added points between existing points 2 times, then turned all segments into straight lines.
# Saved as SVG with absolute coordinates (Preferences > SVG Output > Path Data).
# Loaded coords from SVG source code, remove letters from start and end, and replace " " with ","
coords_f <- c(218.1169,163.46992,215.56952,177.96334,213.51976,189.84421,211.82546,200.33884,210.34442,210.67351,208.24728,226.35176,205.51032,243.54066,201.92029,259.27223,197.26391,270.57846,195.45112,272.90665,193.28288,274.70167,190.97247,275.85687,188.73314,276.26564,187.03291,276.03164,185.79476,275.38887,184.84097,274.42619,183.99382,273.23248,182.45947,271.13533,180.24976,269.10927,177.54243,267.58084,174.51519,266.97658,171.25987,267.58973,169.08867,269.18036,167.87718,271.37526,167.501,273.8012,168.44294,277.0032,171.48203,279.79643,176.93817,281.77214,185.13126,282.52154,191.01986,281.80176,196.83737,279.60686,202.29944,