Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
@alekrutkowski
alekrutkowski / rstudio_bindings.json
Last active October 27, 2022 15:04
RStudio key bindings / shortcuts. See the entry for "commentUncomment" – multiple bindings possible. To find out where to save, see section "Saving and Loading" in https://support.rstudio.com/hc/en-us/articles/206382178-Customizing-Keyboard-Shortcuts-in-the-RStudio-IDE#:~:text=Saving,Kevin
{
"goToFileFunction": "Ctrl+P",
"insertPipeOperator": "Ctrl+Shift+M",
"restartR": "Ctrl+R",
"activateTerminal": "Ctrl+3",
"browseAddins": "",
"closeProject": "",
"closeTerminal": "",
"sendTerminalToEditor": "",
"newTerminal": "Ctrl+T",
@alekrutkowski
alekrutkowski / rglue.R
Created October 9, 2022 08:32
Recursive `glue::glue`
# Recursive `glue::glue` based on
# https://github.com/SuperMayo/gonfig/blob/master/R/glue.R
# but simplified
rglue <- function(string, ...) {
glued <- glue::glue(string, ...)
if (glued==string) glued else rglue(glued, ...)
}
@alekrutkowski
alekrutkowski / emailAddressToInstitutionName.R
Created September 29, 2022 13:54
R function: email address ➜ institution name, quick and dirty (just the 1st hit in Bing)
# Usage example (disclaimer: fictitious email addresses, any resemblance to real ones is coincidental):
## emailAddressToInstitutionName(c('[email protected]',
## '[email protected]'))
##> "Ministère du Travail, du Plein emploi et de l'Insertion"
##> "Ministerie van Sociale Zaken en Werkgelegenheid | Rijksoverheid.nl"
# Required packages: `magrittr`, `rvest`
library(magrittr) # for the %>% operator
emailAddressToInstitutionName <- function(charvec_of_email_addresses,
@alekrutkowski
alekrutkowski / mschart_examples.R
Created September 27, 2022 14:10
Examples of native Microsoft Office charts (with embedded data editable with Excel) generated programmatically with R and saved within a Word (docx) file
# `mschart` and `officer` R packages are required
# `magrittr` required for the %>% pipe
scatter_plot <-
mschart::ms_scatterchart(
data=iris, x="Sepal.Length",
y="Sepal.Width", group="Species"
) %>%
mschart::chart_settings(scatterstyle="marker")
@alekrutkowski
alekrutkowski / multiple_dispatch.hs
Last active August 17, 2022 19:16
Multiple dispatch (double dispatch) in Haskell
main = do
putStrLn $ show $ add (MyInt 1) (MyInt 3)
-- MyInt 4
putStrLn $ show $ add (MyInt 13) (MyString "B")
-- MyString "13B"
putStrLn $ show $ add (MyString "B") (MyInt 13)
-- MyString "B13"
putStrLn $ show $ add (MyString "a") (MyString "b")
-- MyString "ab"
putStrLn $ show $ add' $ IntInt 1 3
@alekrutkowski
alekrutkowski / .Rprofile
Last active November 17, 2022 16:31
Functions and active bindings for rapid interactive work in R
options(width=200)
if (interactive()) {
message('================================================================================')
ab <- function(sym_as_str, fun)
makeActiveBinding(sym_as_str, fun, .GlobalEnv)
library(magrittr)
message('- Imported package `magrittr`')
library(data.table)
@alekrutkowski
alekrutkowski / import_AMECO.R
Last active October 5, 2022 15:18
R script to import European Commission's (DG ECFIN's) Annual Macro-ECOnomic database (AMECO)
library(magrittr)
library(data.table)
download.file('https://ec.europa.eu/economy_finance/db_indicators/ameco/documents/ameco0.zip',
'ameco0.zip')
time_stamp <-
'https://ec.europa.eu/info/business-economy-euro/indicators-statistics/economic-databases/macro-economic-database-ameco/download-annual-data-set-macro-economic-database-ameco_en' %>%
rvest::read_html() %>%
rvest::html_elements(xpath='//*[@id="block-ewcms-theme-main-page-content"]/article/div/div/div/div[2]/div/div/p[1]/text()[2]') %>%
rvest::html_text() %>%
@alekrutkowski
alekrutkowski / eurodata.extras.R
Created May 31, 2022 13:45
Additional functions extending the R package `eurodata`
library(magrittr)
monthToQuarter <- function(charvec, safe=TRUE) {
# monthToQuarter(c('2022M01','2022M02','2022M03','2022M04',
# NA_character_,'2022M12'))
# # "2022Q1" "2022Q1" "2022Q1" "2022Q2" NA "2022Q4"
if (safe)
stopifnot(all(grepl('^[1-2][0-9]{3}M[0-1][0-9]',
charvec) | is.na(charvec)))
yr <-
@alekrutkowski
alekrutkowski / panel.data.table.R
Last active June 2, 2022 13:33
R functions for panel data extending packages `data.table` and `collapse`
# ## Example: ⮦ time=3 missing
# library(magrittr)
# data.table(time = c(1,2, 4,5,6,
# 1,2),
# my_x = c(11:15,
# 11,12),
# my_y = c(101,103,105,NA,109,
# 111,121),
# group = c(rep.int('a',5),
# rep.int('b',2))) %>%
@alekrutkowski
alekrutkowski / eurostat_extended.py
Created May 18, 2022 09:49
Additional functions to `eurostat` Python package (https://pypi.org/project/eurostat)
import eurostat
import pandas as pd
def importData(EurostatDatasetCode, flags=False):
"""
Import a dataset from Eurostat as a flat/melted table (pandas dataframe)
Parameter
----------
EurostatDatasetCode : str