Skip to content

Instantly share code, notes, and snippets.

@jrosell
jrosell / dictionaries.md
Last active February 12, 2026 13:53
Do you want to merging R objects like dictionaries in R? It seems like using base vectors are the fastest of the explored options to merge like dictionaries in R. 15 times faster than purrr::list_modify, fastmap or collections::dict
title format
Merging R objects like dictionaries in R
html

Let's explore how to merge lists, enviroments or vectors like dictionaries in R.

pak::pak(c(
  "purrr",
@jrosell
jrosell / image-embeddings.qmd
Last active February 3, 2026 16:44
Create and search image embeddings using torch, mobilenet v3, rchroma and R
---
title: "Create and search image embeddings using torch, mobilenet v3, rchroma and R"
---
Prepare torch, and mobilenet v3 embeddings.
```{r}
if (!rlang::is_installed("torch")) {
# https://torch.mlverse.org/docs/articles/installation#pre-built
options(timeout = 90000)
@jrosell
jrosell / r.json
Created January 9, 2026 09:02
R snippets for code autocompletion. Copy-paste-modify cases are good snippets candidates.
{
"lib": {
"prefix": "lib",
"body": "library(${1:package})",
"description": "Attach an R package"
},
"src": {
"prefix": "src",
"body": "source(\"${1:file.R}\")",
"description": "Source an R file"
@jrosell
jrosell / reduce-loop-using-pipes.R
Last active December 19, 2025 12:31
Reduce loops using purrr base R pipe. Inspired by https://masalmon.eu/2023/07/26/reduce/
library(purrr)
# The intial information
elements <- tibble::tribble(
~key , ~values ,
"Barbie" , "shoes" ,
"Oppenheimer" , "history"
)
# Aditional values we want to proces
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(rextendr)
library(mirai)
rextendr::rust_source(
file = "rust/xymatch_loop.rs",
module = "xymatch_loop",
profile = "release"
)
rextendr::rust_source(
file = "rust/xymatch_binary.rs",
module = "xymatch_binary",
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 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",
@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
#> 
#>