Skip to content

Instantly share code, notes, and snippets.

View alekrutkowski's full-sized avatar

alek alekrutkowski

View GitHub Profile
@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),
@alekrutkowski
alekrutkowski / pano_image_to_panning_video.py
Created October 29, 2023 17:56
Panoramic Image (JPEG) to panning video (MP4) converter
# Made with ChatGPT
# Required packages:
# - Pillow
# - imageio
# - imageio-ffmpeg
# - numpy
# Usage example:
# create_panning_video('My_Pano_image.jpg', 'My_Pano_video.mp4', 90, 60)
from PIL import Image
import imageio
@alekrutkowski
alekrutkowski / CallPlumberAPI.vba
Last active March 1, 2024 13:37
Calling arbitrary R code from Excel with easy data input and output (Excel↔R)
Sub CallPlumberAPI()
Dim SERVER_URL As String
Dim xmlhttp As Object
Dim ws As Worksheet
Dim inputRange As Range, dataRange As Range, outputRange As Range
Dim cellAddress As String, rCode As String, outputAddress As String
Dim i As Long, j As Long
Dim tsvData As String, processedData As String
Dim outputData() As String, outputRow() As String
@alekrutkowski
alekrutkowski / AttachLabelsToPointsAvoidingOverlap.vba
Last active December 4, 2024 15:16
Excel macro for adding labels to scatterplot points while avoiding their overlap if points are close
Sub AttachLabelsToPointsAvoidingOverlap()
' Based on:
' https://support.microsoft.com/en-gb/topic/how-to-use-a-macro-to-add-labels-to-data-points-in-an-xy-scatter-chart-or-in-a-bubble-chart-in-excel-0f7642a5-fc9f-375c-94f1-953fb55eae06
' but significantly extended to reduce the overlap of labels for points which are close
'
' IMPORTANT: The labels for the scatterplot points are assumed to be in the column directly on the left side of the data range
' USAGE: Select the chart (scatterplot) concerned, then run the macro
' If you don't like the positioning of the labels, simply re-run the macro - new positions may be better
' because it will be based on new random numbers.
@alekrutkowski
alekrutkowski / calculate_ram_usage.sh
Created June 17, 2023 19:51
A bash script to add RAM usage percent as a new item in the XFCE panel
#!/bin/bash
# Prerequisities
# 0) XFCE
# 1) Save this script in your home directory as: /home/your_user_name/calculate_ram_usage.sh
# 2) Add a new XFCE Generic Monitor Applet item (with the settings: no label, 1 second refresh period): /home/your_user_name/calculate_ram_usage.sh
output=$(free)
# Use grep with perl-regex (-P) to extract numbers and store them in an array
numbers=($(echo "$output" | grep -oP '\d+'))
@alekrutkowski
alekrutkowski / README.md
Last active June 7, 2023 16:07
A simple [shinylive](https://shiny.posit.co/py/docs/shinylive.html) app example with Excel input, dataframe processing, and Excel output

To compile it into a docs folder/directory which could be hosted as an app on GitHub (Pages), assuming that the app.py and requirements.txt are in the excel_io folder/directory:

shinylive export excel_io docs

To run it (after compilation) locally:

python -m http.server --directory docs 8008

The input Excel file (used as an example) that is uploaded by a user in the running app has the following contents:

@alekrutkowski
alekrutkowski / Copy-FileWithTimestampPrefix.ps1
Created May 22, 2023 08:49
Copy file with timestamp prefixed to its name before saving in the destination (Date and Time when File Created)
## Code generated by the free-access ChatGPT May 12 Version
## Usage example:
# Copy-FileWithTimestampPrefix -SourceFilePath "F:\PRIVATE\M4ROOT\CLIP\C0040.MP4" -DestinationFolderPath "B:\Videos"
## File copied to: B:\Videos\2023-05-17_15.43.50__C0043.MP4
function Copy-FileWithTimestampPrefix {
param (
[Parameter(Mandatory = $true)]
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[string]$SourceFilePath,
@alekrutkowski
alekrutkowski / InfixFunctions.jl
Last active February 24, 2023 15:59
A way to define descriptive infix operators in Julia, similar to R's user-defined %-delimited operators (https://adv-r.hadley.nz/functions.html#infix-functions)
# Copied from https://github.com/Ismael-VC/InfixFunctions.jl/blob/master/src/InfixFunctions.jl
# and updated for compatibility with current Julia (ver. 1.8.5), specifically method `isdefined`.
# See the usage examples below.
# Before using:
# 1) Download this file to your current working directory, e.g. in Julia
# julia> run(`wget https://gist.githubusercontent.com/alekrutkowski/32097fe6502c254374c406fbb71d3ef8/raw/InfixFunctions.jl`)
# 2) Type in Julia:
# julia> include("InfixFunctions.jl")
# julia> using Main.InfixFunctions
@alekrutkowski
alekrutkowski / openxlsx2.extras.R
Last active November 22, 2022 08:44
Helper functions to the R package `openxlsx2`
library(openxlsx2)
library(magrittr) # for the %>% operator
# Import all sheets in the Excel file as a list of data.frames
# like in the previous package version (openxlsx)
readAllSheets <- function(xlsx_file_name, drop_empty_sheets=TRUE,
suppress_warnings=TRUE, ...)
xlsx_file_name %>%
read_sheet_names() %>%
sapply(function(x)
@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",