Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
# Code generated with eurodata_codegen on 2024-09-18 16:26:27 (UTC+2:00, Central European Summer Time)
library(magrittr)
library(data.table)
library(eurodata)
library(openxlsx2)
dt__lfsi_emp_a <-
## Link to filtered raw data (TSV):
# https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/LFSI_EMP_A/.EMP_LFS.T.Y20-64.PC_POP.EU27_2020?format=TSV
## Meaning of the codes in `filters` below:
@alekrutkowski
alekrutkowski / helper_functions.R
Last active September 25, 2024 14:52
ESSPROS social protection expenditure real growth decomposition including early estimates
renameColumns <- function(dt, ...) {
pairs <-
substitute(list(...)) %>%
as.list %>%
tail(-1) %>%
lapply(. %>% as.list %>% tail(-1) %>% rev)
from <-
pairs %>%
sapply(. %>% .[[1]] %>% as.character)
to <-
@alekrutkowski
alekrutkowski / data.table_setnames_extensions.R
Created September 6, 2024 08:23
R functions – convenient extensions of data.table's `setnames` function
library(magrittr)
library(data.table)
setnamesWithArrows <- function(dt, ...) {
pairs <-
substitute(list(...)) %>%
as.list %>%
tail(-1) %>%
lapply(. %>% as.list %>% tail(-1) %>% rev)
from <-
@alekrutkowski
alekrutkowski / namedList.R
Last active August 28, 2024 08:09
R function to avoid the repetitions in situations like `list(first = first, second = second, third = third)`
namedList <- function(...) {
# Capture the variable names as symbols
# and convert symbols to character names
var_names <- as.character(as.list(substitute(list(...)))[-1])
# Create a named list
stats::setNames(mget(var_names, envir = parent.frame()), var_names)
}
### Usage example
@alekrutkowski
alekrutkowski / webR.download.file.R
Created May 6, 2024 07:18
`download.file` function compatible with webR (CORS)
webR.download.file <- function(url, destfile)
download.file(paste0("https://corsproxy.io/?",URLencode(url)),
destfile)
# See https://github.com/r-wasm/webr/issues/252#issuecomment-1690142510
@alekrutkowski
alekrutkowski / infix_operators.R
Last active May 12, 2024 11:14
Frequently used R infix operators
`%not in%` <- Negate(`%in%`)
`%without%` <- setdiff
`%++%` <- paste0
`%is%` <- identical
@alekrutkowski
alekrutkowski / insheet_eurostat.ado
Last active July 24, 2024 09:08
Stata command to download and import a Eurostat dataset
// Usage example:
// Either the parameter is Eurostat's "online data code":
// insheet_eurostat nama_10_gdp
// or the parameter is the custom URL generated with Eurostat GUI top right corner:
// Go to e.g. https://ec.europa.eu/eurostat/databrowser/view/nama_10_gdp/default/table?lang=en and
// do your selections by clicking first on the circle with plus (+) and then click
// ⤓ Download > ⚙ Options and other formats > Select file format: Text (.tsv, .csv), and Select: Data scope = "Data on this page only" and Compressed = No > Click: [⧉ Copy Link] button
// insheet_eurostat https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/nama_10_gdp/A.CP_MEUR.B1G+P3.EU27_2020+BE+BG+CZ+DK/?format=TSV&startPeriod=2021&endPeriod=2023
// To use it like other commands, put it in your "personal" folder/directory
// (see: https://www.stata.com/support/faqs/programming/personal-ado-directory/ and https://www.stata.com/help.cgi?personal).
@alekrutkowski
alekrutkowski / index.html
Last active April 23, 2024 09:12
Keyboard Shortcut Cheatsheet Maker (made entirely with ChatGPT 4), hosted at https://shiny-r.dev/cheatsheet-maker/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Keyboard Shortcut Cheatsheet Generator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
@alekrutkowski
alekrutkowski / app.R
Last active May 17, 2024 09:30
JAF2R_SpecGen – JAF specification generator for indicators based on Eurostat data for JAF2R (https://github.com/alekrutkowski/JAF2R)
options(shiny.sanitize.errors = FALSE)
library(shiny)
library(data.table)
library(eurodata)
library(magrittr)
MetaBase <-
importMetabase() %>%
@alekrutkowski
alekrutkowski / listWithoutEmptyArgs.R
Last active March 13, 2024 13:59
R function for dealing with (ignoring) missing/empty arguments inside ellipsis (...)
listWithoutEmptyArgs <- function(...)
eval(Filter(\(x) !identical(as.character(x), "") || identical(x,""),
bquote(.(substitute(list(...))))))
# > list(a=1,,b=2:10)
# Error in list(a = 1, , b = 2:10) : argument 2 is empty
# > listWithoutEmptyArgs(a=1,,b=2:10)
# list(a = 1, b = 2:10)