Skip to content

Instantly share code, notes, and snippets.

View eliocamp's full-sized avatar

Elio Campitelli eliocamp

View GitHub Profile
@eliocamp
eliocamp / ditto.R
Last active November 5, 2020 21:25
ditto <- function(expr) {
plus_fun <- function(e1, e2) {
if (is.list(e1)) {
c(e1, list(e2))
} else {
list(e1, e2)
}
}
@eliocamp
eliocamp / lernr_translate.R
Created October 4, 2020 16:05
Code to try to translate learnr's UI
tutorial.js <- system.file("lib", "tutorial", "tutorial.js", package = "learnr")
tutorial_format.js <- system.file("rmarkdown", "templates", "tutorial", "resources", "tutorial-format.js", package = "learnr")
changes <- list(
run_code = list(
text = "Correr Código",
file = tutorial.js,
pattern = 'var run_button = add_submit_button("fa-play", "btn-success",',
code = 'var run_button = add_submit_button("fa-play", "btn-success", "{text}", false)'
@eliocamp
eliocamp / facet_labels
Created July 15, 2020 20:39
Adds labels to facets of a ggplot plot
#' Adds tags to facets
#'
#' @export
facet_tags <- function(position = "tl", tag_pool = letters, open = "", close = ")", stroke = 0.15, stroke.color = "white",
color = "black") {
if (!is.list(position)) {
position <- switch(position,
tl = list(x = 0, y = 1, hjust = 0, vjust = 1),
tr = list(x = 1, y = 1, hjust = 1, vjust = 1),
bl = list(x = 0, y = 0, hjust = 0, vjust = 0),
@eliocamp
eliocamp / system_deps.R
Created June 30, 2020 17:00
Get system dependencies using sysreqs.r-hub.io
## List system dependenceis for a local project (with a DESCRIPTION file)
## and create an "apt install" command that installs them. Put it in your
## dockerfile and be happy.
library(magrittr)
all_types <- c("Imports", "Depends", "Suggests")
description_file <- here::here("DESCRIPTION")
direct_deps <- all_types %>%
@eliocamp
eliocamp / tunnel-rstudio-server.sh
Created May 28, 2020 00:58
Crea un comando para armar un tunnel y abrir rstudio server
#!/bin/bash
read -p "Usuario en el servidor: " USUARIO
read -p "Servidor: " SERVER
read -p "Puerto [4572]: " PUERTO
PUERTO=${PUERTO:-4572}
read -p "Nombre del tunel (y el comando) [rstudioserver]: " NOMBRE
NOMBRE=${NOMBRE:-rstudioserver}
library(data.table)
library(magrittr)
library(ggplot2)
url_movility <- "https://raw.githubusercontent.com/githubmg/reduccion_movilidad/master/data/Global_Mobility_Report.csv"
url_deaths <- "https://raw.githubusercontent.com/githubmg/reduccion_movilidad/master/data/time_series_covid19_deaths_global.csv"
mov <- fread(url_movility) %>%
@eliocamp
eliocamp / cdor.R
Last active March 14, 2020 23:09
Possible cdo wrapper in R.
# This is an experiment to wrap CDO in R in a intuitive and pipe friendly way.
# Usage:
# cdo_* will create a string with a commmand. If any of the "input_file" arguments
# is a cdo command, it will append "-" to it so that it can be chained.
# This is good for efficiency as it will allow you to apply multiple
# operations without having to write to disk the intermediate files.
## "Internal" functions in which the magic happens
.enchain <- function(input) {
@eliocamp
eliocamp / babynames.R
Created October 28, 2019 00:41
Trends in gender neutral names in the US
library(ggplot2)
library(data.table)
library(magrittr)
# From https://www.ssa.gov/oact/babynames/limits.html
file <- "https://www.ssa.gov/oact/babynames/state/namesbystate.zip"
destination <- "/home/elio/Downloads/babies.zip"
download.file(file, destination)
dir <- "/home/elio/Downloads/babies"
@eliocamp
eliocamp / pipes_brackets.R
Created July 10, 2019 18:56
pipes vs data.table
library(data.table)
library(magrittr)
library(ggplot2)
set.seed(42)
do_bench <- function(N) {
dt <- suppressWarnings(data.table(x = rnorm(N),
group = letters[1:3]))
pipe <- function() {
dt %>%
@eliocamp
eliocamp / tweet_storm.R
Last active October 10, 2022 16:02
Example of posting a twitter thread from R
---
title: "Tweet thread"
author: "Elio Campitelli"
output: github_document
---
```{r}
knitr::opts_chunk$set(dev = "png",
tweet_this = TRUE)