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 / backup-db.R
Created April 12, 2025 18:27
Backup a sqlite database using Rhttps://mastodon.social/@gvwilson/114316283939691522
library(RSQLite)
db_disk <- dbConnect(SQLite(), "/path/to/database.sqlite3")
db_mem <- dbConnect(SQLite(), ":memory:")
sqliteCopyDatabase(db_disk, db_mem)
library(shiny)
library(calcite)
library(htmltools)
library(arcgisutils)
# read in our sample dataset
earthquakes <- sf::st_read(
"https://github.com/R-ArcGIS/calcite/raw/refs/heads/main/dev/earthquakes.fgb"
)
@JosiahParry
JosiahParry / write-geoparquet.R
Created March 28, 2025 22:18
Creating GeoParquet for Google BigQuery
library(arrow)
library(geoarrow)
library(sf)
# following example here: https://geoarrow.org/geoarrow-r/index.html
nc <- read_sf(system.file("gpkg/nc.gpkg", package = "sf"))
tf <- tempfile(fileext = ".parquet")
# this creates a GeoParquet file
nc |>
@JosiahParry
JosiahParry / read-r-headers.R
Last active March 26, 2025 15:38
Read R’s C headers to identify where R functions are coming from
library(dplyr)
api <- as_tibble(tools:::funAPI())
# list all header files from include dir
header_files <- list.files(
R.home("include"),
full.names = TRUE,
recursive = TRUE,
@JosiahParry
JosiahParry / main.rs
Created March 15, 2025 16:52
Generate a random available user port in Rust
use rand::{rng, seq::SliceRandom};
use std::{
io::Result,
net::{IpAddr, SocketAddr, TcpListener, ToSocketAddrs},
vec::IntoIter,
};
pub struct RandomUserPort(std::ops::RangeInclusive<u16>);
impl RandomUserPort {
@JosiahParry
JosiahParry / list-all-files.R
Created February 9, 2025 19:36
Function to list all files based on globs, file names, and directories.
list_all_files <- function(include = "*") {
# list all files in the current directory recursing
all_files <- fs::dir_ls(recurse = TRUE, all = TRUE, type = "any")
# Identify directory patterns explicitly mentioned
matched_dirs <- include[fs::dir_exists(include)]
# Get *all* files inside matched directories
extra_files <- unlist(
lapply(
@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,