Skip to content

Instantly share code, notes, and snippets.

View jrosell's full-sized avatar

Jordi Rosell jrosell

View GitHub Profile
library(S7)
ScalarDouble <- new_class("ScalarDouble",
properties = list(value = class_double),
constructor = function(value) {
stopifnot(is.double(value), length(value) == 1)
new_object(ScalarDouble, value = value)
}
)
@jrosell
jrosell / compute-risk-ratio-risk-difference.R
Last active September 26, 2025 07:36
Methods for computing Risk Ratio and Risk Difference.
stopifnot(requireNamespace("rlang"))
rlang::check_installed("pak")
pkgs <- rlang::chr(
"tidymodels",
"marginaleffects",
"parameters",
"modelbased",
"dplyr",
"stringr",
"tibble",
@jrosell
jrosell / async-json-api-plumber2-mirai-S7-DBI.R
Last active February 20, 2026 21:45
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",
@jrosell
jrosell / cocktails_dplyr_replace_values.md
Created September 18, 2025 07:42
Example of use case for the dplyr::replace_values function.
# Cocktails example by Hadley in 2021: Video https://youtu.be/kHFmtKCI_F4?si=HC4S3B-RI_l96sU4&t=1810 | Code: https://gist.github.com/hadley/a892ff8f00973e3bc864851deae8315f
# Added: New version using the replace_values function.

# Installing the new branch with the new feature
pak::pak("tidyverse/dplyr@feature/case-family")
#> ℹ Loading metadata database
#> ✔ Loading metadata database ... done
#> 
#> 
rlang::check_required(c(
  "patchwork",
  "readxl",
  "tidyverse",
  "pak",
  "showtext",
  "systemfonts",
  "sysfonts"
))

ggplot2 (R)

library(ggplot2)

ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  geom_smooth(se = FALSE)
@jrosell
jrosell / faster-small-ollama-llm-models.md
Last active September 3, 2025 15:27
What are the fastest small ollama models for quick LLM tasks?
pak::pak(c("ollamar", "ellmer", "tidyverse"))
library(ellmer)
library(tidyverse)

all_models <- c(
  "qwen2.5:0.5b",
  "qwen2.5-coder:3b",
  "deepseek-coder:1.3b",
  "internlm2:1m",
@jrosell
jrosell / pipeline.R
Last active August 30, 2025 19:26
Example of a machine learning pipeline in Python using dagster to skip the recomputation of assets if last modification dates are not updated from since previous computations. Also an example in R using the {targets} package.
library(targets)
library(tibble)
library(readr)
library(glmnet)
# Step 1: load raw data
load_raw_data <- function() {
Sys.sleep(10) # simulate slow load
tibble(
library(tidyverse)
library(tidymodels)
set.seed(123)
n <- 10000
Evento1 <- rbinom(n, size = 1, prob = 0.7) 
Evento2 <- rbinom(n, size = 1, prob = 0.6) 
p_target <- ifelse(Evento1 == 1, 0.7, 0.2)
p_target <- p_target + ifelse(Evento2 == 1, 0.3, -0.05)
p_target <- pmin(pmax(p_target, 0.01), 0.99) # limitar entre 0 y 1
@jrosell
jrosell / experiments_with_binary_operators.R
Last active August 1, 2025 17:13
Inspired by rust and haskell.
# getUser cfg =
# lookup "username" cfg >>= \uname ->
# lookup "age" cfg >>= \ageStr ->
# readMaybe (trim ageStr) >>= \age ->
# lookup "email" cfg >>= \emailRaw ->
# validateEmail (trim emailRaw) >>= \email ->
# Just (User uname age email)
# 1. Using atomic vectors -----