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
# Usage: | |
# variable %<j% long_running_function() | |
`%<j%` <- function(x, y) { | |
var_name <- paste0(deparse(substitute(x))) | |
expr <- paste0(deparse(substitute(y)), collapse = "\n") | |
script <- tempfile() | |
lines <- writeLines(paste0(var_name, " <- ", expr), script) | |
rstudioapi::jobRunScript(script, |
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(data.table) | |
library(ggplot2) | |
library(metR) | |
library(magrittr) | |
library(gganimate) | |
# Define la función que genera el campo vector | |
funcion_campo <- function(x, y, a, b) { | |
list(dx = 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
# Y = data matrix. | |
# yearly u anomalies for the northern hemisphere between 1948 and 2010. | |
# 63 rows and 5328 columns (2.5 degree grid) | |
# | |
# X = timeseries matrix. | |
# column vector with values 1948 to 2010 | |
# 63 rows 1 column | |
N <- nrow(X) | |
# Singular value decomposition |
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(ecmwfr) | |
wf_set_key(service = "webapi") | |
request <- list( | |
class = "ei", | |
dataset = "interim", | |
expver = "1", | |
date = "19790101", | |
grid = "0.75/0.75", |
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
args <- function(...) { | |
dots <- match.call(expand.dots = FALSE)$`...` | |
class(dots) <- c("lambda", "arguments", "pairlist") | |
dots | |
} | |
body <- function(...) { | |
fun <- rlang::new_function(alist(.x =, .y = ), body = substitute(...)) | |
class(fun) <- c("lambda", "body", class(fun)) |
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(ggplot2) | |
# Creo datos sintéticos para demostración | |
set.seed(42) # para que sea reproducible | |
N <- 10 # 10 puntos | |
temps <- data.frame(lat = runif(N, -55, -20), # entre 55°S y 20°S | |
lon = runif(N, 360-80, 360-55), # entre 80°O t 55°O | |
t = rnorm(N, 15)) # con temperatura promedio 15°C | |
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(lubridate) | |
library(data.table) | |
Trajectory <- function(formula, x0, y0, data = NULL, res = 0.5) { | |
dep.names <- formula.tools::lhs.vars(formula) | |
if (length(dep.names) == 0) stop("LHS of formula must have at least one variable") | |
ind.names <- formula.tools::rhs.vars(formula) | |
if (length(ind.names) > 3) { | |
stop("RHS of formula must be of the form x + y + t") |
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
# All this is implemented (plus bugfixes!) in the ggnewscale package: | |
# https://github.com/eliocamp/ggnewscale | |
# If you have any issues, I prefer it if you send them as issues here: | |
# https://github.com/eliocamp/ggnewscale/issues | |
#' Allows to add another scale | |
#' | |
#' @param new_aes character with the aesthetic for which new scales will be | |
#' created | |
#' |
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
geom_relief <- function(mapping = NULL, data = NULL, | |
stat = "identity", position = "identity", | |
..., | |
anglebreaks = 60, | |
sunangle = 60, | |
shadow = TRUE, | |
maxsearch = 100, | |
lambert = TRUE, | |
zscale = 1, | |
multicore = 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
library(ggplot2) | |
url <- "https://ih0.redbubble.net/image.512523322.6908/flat,800x800,075,f.jpg" | |
file <- tempfile() | |
download.file(url, file) | |
im <- jpeg::readJPEG(file) | |
im <- apply(im, c(1, 2), sum) | |
im <- im/3 | |
ys <- nrow(im) | |
xs <- ncol(im) |