Skip to content

Instantly share code, notes, and snippets.

View J-Moravec's full-sized avatar

Jiří Moravec J-Moravec

View GitHub Profile
which_min = function(x){
i = which.min(x)
m = x[i]
c("min" = m, "index" = i)
}
closest_tss = function(x, tss){
# x needs to be a single row
@J-Moravec
J-Moravec / which_min.r
Last active June 2, 2025 11:56
C vs base R
.which_min = inline::cfunction(
sig = signature("vec" = "SEXP"),
body = '
int* v = INTEGER(vec);
int min = v[0];
size_t index = 1;
size_t n = (size_t) XLENGTH(vec);
for(int i = 0; i < n; i++){
if(v[i] < min){
min = v[i];
@J-Moravec
J-Moravec / gist:5a1d60ac78c1fb8a0d2a100c5b7b6e0e
Created April 17, 2025 04:09
Copilot compares mutr, testthat, and tinytest
Comparison of mutr, testthat, and tinytest
Mutr
Purpose: A minimalistic unit-testing framework designed for simplicity and ease of integration into small projects.
Features: Provides core testing functions (TEST, TEST_SET, TEST_ERROR), simple test reporting, and minimal dependency (base R).
Advantages: Extremely lightweight and easy to use for self-contained or basic testing scenarios.
Limitations: Lacks advanced features like mocking, test parameterization, or integration with CI systems, making it unsuitable for complex projects.
Testthat
Purpose: The most popular and feature-rich testing framework for R, designed for comprehensive testing of R packages.
@J-Moravec
J-Moravec / serve.r
Last active December 8, 2024 05:07
Implementation of a local server for static website using the R's internal httpd server.
error404 = paste0(
"<!DOCTYPE html>",
"<html lang=\"en\">",
"<head>",
" <meta charset=\"UTF-8\">",
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">",
" <title>Resources not found</title>",
"</head>",
"<body>",
" <div class=\"main\">",
@J-Moravec
J-Moravec / install.sh
Created November 15, 2024 08:44
Install unison with unison-fsmonitor
# unison-fsmonitor is not available in the repositories
# it is reported and discussed for like 8 years already
# there is no hope, only manual compile
#!/bin/bash
UNISON_VERSION=2.51.5 # version in Ubuntu repo
echo "Install Unison." \
&& apt install wget ocaml \
&& pushd /tmp \
&& wget https://github.com/bcpierce00/unison/archive/v$UNISON_VERSION.tar.gz \
@J-Moravec
J-Moravec / is_odd.r
Created November 8, 2024 02:46
Is odd: bitwise or modulo?
is_odd_bitwise = \(x) bitwAnd(x, 1L)
is_odd_mod = \(x) x %% 2L
s = sample(1:1e7)
bench::mark(is_odd_bitwise(s), is_odd_mod(s))
@J-Moravec
J-Moravec / gist:27ce2eaee75504febb8ab1015bc24f6d
Created September 17, 2024 20:10
An example of subclassing conditions and then handling conditions of only a particular class.
# constructor based on the simpleError function
my_error = function(message, call = NULL){
class = c("my_error", "error", "condition")
structure(
list(as.character(message), call = call),
class = class
)
}
# my_error still needs to be signaled with stop():
count_to = function(x = 5){
current = 0
function(){
if(i >= x)
return(NULL)
i <<- i+1
i
}
}
@J-Moravec
J-Moravec / sra.r
Created May 24, 2021 23:58
Functions for downloading from SRA database according to GSM sample ID
#' sra.r
#'
#' Function for a programatical access to a SRA and GEO databases
#'
#' depends on rentrez, matrittr and xml packages
#' externally depends on sratools, all sratools must be available in path
import::here("magrittr", "%>%")
import::here("xml2", "read_xml", "as_list")
@J-Moravec
J-Moravec / OrdinalFailSample.xml
Last active April 27, 2021 01:03
An example of XML with randomly generated sequences and ordinal model with different number of states than in the data.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beast namespace="beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood" required="" version="2.6">
<data id="test" dataType="standard">
<sequence taxon="A">0325001134145320315521441333450540150340142324031053552343434540345422555515404425225332030455334311</sequence>
<sequence taxon="B">3303111402245533434135501501430403113451242121243211312030250523440504251354554250554133235411202323</sequence>
<sequence taxon="C">0203545342141415331211042134245105533321245534032452154450211324241122021354024412103533542044452213</sequence>
<sequence taxon="D">5321401114230445532511142242040052431341215151200525030545523403103545524535302424314202314031013500</sequence>
<sequence taxon="E">00433510520542445013055315141311044142443015520443332013354310110010033533343440431233501314422223