Skip to content

Instantly share code, notes, and snippets.

@jrosell
jrosell / ai-evals.R
Created July 2, 2025 16:36
Do you know that you can evaluate IA models and compare their performance? Here an example using the {vitals} package from @posit_pbc by @simonpcouch Evals, evals, evals
rlang::check_installed(c("vitals", "ellmer", "dplyr", "ggplot2"))
library(vitals)
library(ellmer)
library(dplyr)
library(ggplot2)
eval_df <- tibble(
input = c("What's 2+2?", "What's 2+3?", "What's 2+4?"),
target = c("4", "5", "6")
@jrosell
jrosell / formula_typed_functions.R
Last active July 1, 2025 22:22
An experiment creating new functions with type validation usinf formulas
# An experiment creating new functions with type validation using formula notation in R.
# For example, for two double argugment inputs and integer output the formula would be: integer ~ double + double
library(rlang)
library(testthat)
type_check_fn <- function(type_name) {
switch(as.character(type_name),
"integer" = is.integer,
"double" = is.double,
@jrosell
jrosell / run_setup.qmd
Last active June 20, 2025 17:13
Reset the environment variables and execute the setup chunk from other R chunks in quarto. USE WITH CAUTION AT YOUR OWN RISK
rm(list = ls()); rstudioapi::getActiveDocumentContext()$path |>
parsermd::parse_rmd(parse_yaml = FALSE) |>
parsermd::rmd_select("setup") |>
parsermd::as_document() |>
purrr::keep(\(x) !stringr::str_detect(x, '^```|^#')) |>
parse(text = _) |>
eval()
@jrosell
jrosell / google-ads-2-google-sheets.js
Created June 16, 2025 12:49
Script in Google Ads account to export data to a Google Sheets
function main() {
var spreadsheetUrl = 'https://docs.google.com/spreadsheets/d/id/edit?usp=sharing';
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
var pivotedSheet = getOrCreateSheet(spreadsheet, 'PivotedConversionActions'); // New pivoted data sheet
var firstDate = '2022-01-01';
var startDate = getDateNDaysAgo(200);
var endDate = getDateNDaysAgo(1);
rlang::check_installed(c("yyjsonr", "plumber2", "httr2", "bench", "callr", "palmerpenguins", "ggbeeswarm", "ggplot2"))
library(plumber2)
library(palmerpenguins)
data(package = 'palmerpenguins')
get_penguins <- \(n = 100) {
np <- nrow(penguins)
idx <- sample(1:np, n, replace = TRUE)
penguins[idx, ]
library(shiny)
library(dplyr)
library(ggplot2)
library(bslib)
set.seed(42)
data <- data.frame(
category = c("A", "B", "C", "D", "E"),
value = sample(10:100, 5)
)
@jrosell
jrosell / api.domain.com.conf
Last active March 27, 2025 17:41
Example of a JavaScript fetching country code from API. I used a apache2 and ubuntu service for an R API using plumber and ipinfo.io service.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName api.domain.com
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>
@jrosell
jrosell / country_from_coordinates_api.R
Created March 27, 2025 12:26
An example service using R API to obtain the country from latitude and longitude coordinates.
# plumber::plumb("country_from_coordinates_api.R")$run(port = 8000)
library(plumber)
library(httr2)
library(rlang)
#* @serializer html
#* @get /
function() {
html_content <- '<!DOCTYPE html>
@jrosell
jrosell / clickstream_data_analysis.R
Last active March 27, 2025 09:58
An example of data model to perform clickstream data analysis in R for an ecommerce website or app.
``` r
suppressPackageStartupMessages({
library(dplyr)
library(tidyr)
})
click_data <- tibble(
click_id = c("u1", "u1", "u1", "u1", "u1", "u1", "u1", "u1", "u1", "u2", "u2", "u2", "u3", "u3", "u2"),
@jrosell
jrosell / install_and_load.R
Last active June 12, 2025 15:28
This is how I like to check for required installed versions and load packages in my R scripts, including non-CRAN packages. It requires {rlang} and {pak} packages.
if (!requireNamespace('rlang')) stop("Please, run install.packages('rlang')")
rlang::check_required("pak")
pkgs <- rlang::chr(
rlang = "r-lib/[email protected]",
purrr = "purrr",
tidyverse = "tidyverse/tidyverse",
tidymodels = "tidymodels/tidymodels",
jrrosell = "jrosell/jrrosell@main",
)
pak::pak(pkgs)