Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
@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 May 17, 2024 09:17
Stata command to download and import a Eurostat dataset
// Usage example:
// Either the prameter 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:
// Download > Options and other formats > Select file format: Text (.tsv, .csv) and Select: Compressed = No
// 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
prog insheet_eurostat
qui des
if `r(k)'!=0 {
@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)
@alekrutkowski
alekrutkowski / memory_address.R
Last active February 22, 2024 16:22
Get memory address of an R object
memoryAddress <- function(x)
strsplit(capture.output(.Internal(inspect(x))),split=" ")[[1]][1]
### Usage examples
# > abc <- c(1,2,3,10,11)
# > xyz <- abc
# > memoryAddress(abc)==memoryAddress(xyz)
# [1] TRUE
# > xyz <- abc+1
import os
import random
import string
while True:
folder_path = input("Please enter the folder path: ")
# Check if the path is a valid directory
if os.path.isdir(folder_path):
print(f"Valid directory: {folder_path}")
@alekrutkowski
alekrutkowski / async_mirai.R
Last active February 23, 2024 13:39
async/non-blocking programming in R with `mirai`
### Usage:
# a = 1; b = 2
# my_result <- eval.async(a+b)
### when mirai::unresolved(my_result) == FALSE
### you can use my_result$data
### do.call.async can be used like do.call
eval.async <-
@alekrutkowski
alekrutkowski / data.table_filtering_by_groups.R
Last active January 12, 2024 11:37
Concise/terse filtering R data.table rows by condition within groups (inside a magrittr)
library(data.table)
library(magrittr)
## Pattern:
# DT[DT[,row_filtering_expression, by=grouping_column][[2]]]
## or generally
# DT[DT[,row_filtering_expression, by=.(grouping_col1,grouping_col2,etc.)][[number_of_grouping_cols+1]]]
## Example:
data.table(a=c(1,1,1,2,2),