Skip to content

Instantly share code, notes, and snippets.

@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",
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),
@jrosell
jrosell / check_installed_universe_packages.R
Created September 7, 2023 14:35
A way to install r-universe.dev packages using rlang::check_installed to interactively ask if possible, fails if not.
# remove.packages(c("universe", "sfdep"))
rlang::check_installed("universe", action = \(pkg, ...) {
install.packages('universe', repos = c('https://ropensci.r-universe.dev', 'https://cloud.r-project.org'))
})
universe::add("ropensci")
universe::add("josiahparry")
rlang::check_installed("sfdep")
library(tidyverse)
n <- 1000
v <- 1:n
vector_c <- function(n) {
out <- c()
for (i in 1:n) {
out <- c(out, i)
}
@jrosell
jrosell / anomalies.qmd
Last active August 23, 2023 15:57
Detect anomalies over time using percentiles and using a Isolation forest model.
---
title: "anomalies"
format: html
jupyter: python3
editor_options:
chunk_output_type: console
---
Install requisites: