Skip to content

Instantly share code, notes, and snippets.

View JosiahParry's full-sized avatar
🕵️‍♂️
sleuthing data

Josiah Parry JosiahParry

🕵️‍♂️
sleuthing data
View GitHub Profile
@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 June 28, 2024 07:33
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,
@christophergandrud
christophergandrud / source_lines.R
Created December 1, 2014 14:08
Source specific lines from a file in R
#' Source specific lines in an R file
#'
#' @param file character string with the path to the file to source.
#' @param lines numeric vector of lines to source in \code{file}.
source_lines <- function(file, lines){
source(textConnection(readLines(file)[lines]))
}