This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_mode <- \(x) { | |
r <- table(x) | |
names(r[r == max(r)]) | |
} | |
# For example: | |
# > get_mode(c(1,2,2,3,3,4)) | |
# 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Preparations | |
pkgs <- c("tidyverse", "pRecipe", "giscoR", "terra", "rayshader", "patchwork") | |
rlang::check_installed(pkgs) | |
suppressPackageStartupMessages({ | |
invisible(lapply(pkgs, library, character.only = TRUE, quietly = TRUE)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# preallocates memory. | |
fmat <- function(n) { | |
res <- matrix(NA, n, n) | |
for (i in 1:n) { | |
res[i, ] <- runif(n) | |
} | |
res | |
} | |
# rbind each time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
NewerOlder