Skip to content

Instantly share code, notes, and snippets.

View eliocamp's full-sized avatar

Elio Campitelli eliocamp

View GitHub Profile
@eliocamp
eliocamp / hire.R
Created May 17, 2019 18:44
Evaluates expressions in an RStudio Job
# 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,
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,
# 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
@eliocamp
eliocamp / ecmwfr_temperature.R
Created April 7, 2019 00:17
Example ecmwfr
library(ecmwfr)
wf_set_key(service = "webapi")
request <- list(
class = "ei",
dataset = "interim",
expver = "1",
date = "19790101",
grid = "0.75/0.75",
@eliocamp
eliocamp / lambda.R
Created March 13, 2019 20:38
Create functions separating body and arguments
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))
@eliocamp
eliocamp / mapa_ggplot2.R
Last active November 21, 2018 14:22
Mapas en ggplot2
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
@eliocamp
eliocamp / Trajectory.R
Last active October 6, 2018 20:01
Trajectories
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")
@eliocamp
eliocamp / new_aes.R
Last active June 16, 2023 09:07
A way to add multiple color or fill scales to a ggplot2 plot
# 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
#'
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,
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)