Skip to content

Instantly share code, notes, and snippets.

@jrosell
jrosell / async-json-api-plumber2-mirai-S7-DBI.R
Last active January 27, 2026 20:57
Example of an async JSON API in R using dbplyr, SQLite, S7, plumber2 and mirai.
stopifnot(requireNamespace("rlang"))
rlang::check_installed("pak")
pkgs <- rlang::chr(
"rlang" = "rlang",
"plumber2" = "posit-dev/plumber2",
"S7",
"jsonlite",
"httr2",
"mirai",
@erikwestlund
erikwestlund / fancy_r_view!
Last active May 30, 2025 17:41
Fancy R View()
#' Fancy View function that opens a DataTable in the browser using DT package
#'
#' @param x The data to view
#' @param title Optional title for the view
#' @return Opens a browser window with the data in a DataTable
#' @export
fancy_view <- function(x, title = NULL) {
# Check if x is provided
if (missing(x)) {
stop("No data provided to view. Please provide a data frame or other viewable object as the first argument.")
@J-Moravec
J-Moravec / serve.r
Last active December 8, 2024 05:07
Implementation of a local server for static website using the R's internal httpd server.
error404 = paste0(
"<!DOCTYPE html>",
"<html lang=\"en\">",
"<head>",
" <meta charset=\"UTF-8\">",
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">",
" <title>Resources not found</title>",
"</head>",
"<body>",
" <div class=\"main\">",
@debasishg
debasishg / dod.md
Last active January 12, 2026 16:07
Data oriented design, hardware awareness, cache awareness in data structures & algorithms

Performance Engineering, Hardware and cache awareness with algorithm and data structures

  1. Parallel Computing Course - Stanford CS149, Fall 2023
  2. Performance-Aware Programming Series by Casey Muratori
  3. Algorithms for Modern Hardware
  4. Computer Systems: A Programmer's Perspective, 3/E - by Randal E. Bryant and David R. O'Hallaron, Carnegie Mellon University
  5. Performance Engineering Of Software Systems - am MITOCW course
  6. Parallel Programming 2020 by NHR@FAU
  7. Cpu Caches and Why You Care - by Scott Meyers
  8. [Optimizing a ring buffer for throughput](https://rig
enum <- function(...) {
vals <- if (is.null(...names())) {
setNames(seq_len(...length()), c(...))
} else {
c(...)
}
out <- new.env(parent = emptyenv())
list2env(as.list(vals), out)
lapply(names(vals), lockBinding, out)
lockEnvironment(out)
@DavZim
DavZim / plumber.R
Created January 15, 2024 22:20
R Plumber + HTMX
#* @assets ./public /
list()
n_clicked <- 0
#* @post /clicked
#* @serializer html
function() {
n_clicked <<- n_clicked + 1
paste0("Clicked <b>", n_clicked, "</b>\n")
}
@jonocarroll
jonocarroll / copy_on_modify.R
Last active August 29, 2024 20:09
Understanding R's copy-on-modify semantics wrt the symbol table
## Modify a vector in the workspace; x is a user-accessible symbol
x <- 42
.Internal(inspect(x))
# @5631a3a19e20 14 REALSXP g0c1 [REF(5)] (len=1, tl=0) 42
x[1] <- 43 # modification causes a copy (address changes)
.Internal(inspect(x))
# @5631a36c1cb8 14 REALSXP g0c1 [REF(4)] (len=1, tl=0) 43
## Modify a vector inside a function; user cannot access y
f <- function() {
@s-u
s-u / mmap_alloc.c
Created October 22, 2014 15:21
example of using custom mmap allocator in R
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <Rinternals.h>
#include <R_ext/Rallocators.h>
static void *mmap_alloc(R_allocator_t *allocator, size_t length) {