Skip to content

Instantly share code, notes, and snippets.

get_mode <- \(x) {
r <- table(x)
names(r[r == max(r)])
}
# For example:
# > get_mode(c(1,2,2,3,3,4))
# 2 3
#!/usr/bin/env Rscript
cat("==========================================================================\n")
cat("Example of strategy patern in R: Multiple ways to show routes. \n")
# Problem:
# You build a navigation app for casual travelers and help users quickly orient themselves in any city.
#
# - The first only build the routes over roads.
# - Next update, you added an option to build walking routes.
@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"),
@jrosell
jrosell / pluja.R
Last active March 26, 2024 17:14
Adapating code from https://github.com/milos-agathon/precipitation-maps to compare spain precipitation in 2023 and previous years
## Preparations
pkgs <- c("tidyverse", "pRecipe", "giscoR", "terra", "rayshader", "patchwork")
rlang::check_installed(pkgs)
suppressPackageStartupMessages({
invisible(lapply(pkgs, library, character.only = TRUE, quietly = TRUE))
})
# Dades per CCAA ===============================================================
library(tidyverse)
library(rvest)
theme_set(theme_minimal())
older_year <- 2002
region <- tibble(
slug = c("andalucia", "aragon", "asturias", "canarias", "cantabria", "castilla-leon",
"castilla-la-mancha", "cataluna", "ceuta", "melilla", "madrid", "valencia",
@jrosell
jrosell / base-r-summarise.R
Last active March 20, 2024 15:44
How would you write this in base R 4.1+? Is there a better way? More approaches here: https://gist.github.com/hadley/c430501804349d382ce90754936ab8ec
# Single grouping in base R: Multiple functions
library(dplyr, warn.conflicts = FALSE)
expected_output <- mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n()) %>%
as.data.frame()
expected_output
by <- c("cyl")
library(tidyverse)
x <- list(a = list(b = 1, c = NULL), d = NULL)
get_list_names <- \(x, y = c()){
if (!is.null(names(x))){
for (nm in names(x)) {
y <- get_list_names(x[[nm]], c(y, nm))
}
}
@jrosell
jrosell / game-of-life-coro-generators.R
Created February 27, 2024 16:28
Game of Life using {coro} generators in R
# Game of Life using {coro} generators in R
# Inspiration from Jack Diederich's Python implementation: https://gist.github.com/jhrr/2b5a60e9efcb573c9cc4
library(tidyverse)
library(coro)
library(zeallot)
generate_neighbors <- generator(function(point) {
c(x, y) %<-% point
yield(c(x + 1, y))
# preallocates memory.
fmat <- function(n) {
res <- matrix(NA, n, n)
for (i in 1:n) {
res[i, ] <- runif(n)
}
res
}
# rbind each time
@jrosell
jrosell / json-benchmark.R
Last active January 18, 2024 10:34
Comparing the speed of multiple json R packages
# R CMD BATCH --vanilla json-benchmark.R
output_dir <- setwd(here::here())
grDevices::dev.set(1)
# simpleBenchmark
file <- system.file("jsonexamples", "twitter.json", package="RcppSimdJson")
jsontxt <- readLines(file)
res <- microbenchmark::microbenchmark(
jsonify = jsonify::validate_json(jsontxt),
jsonlite = jsonlite::validate(jsontxt),