Skip to content

Instantly share code, notes, and snippets.

View klmr's full-sized avatar
📦
Making your R code easier to reuse

Konrad Rudolph klmr

📦
Making your R code easier to reuse
View GitHub Profile
@klmr
klmr / example.r
Last active January 24, 2024 08:27
R6 classes without the repetition
r6_class(
MyClass,
{
initialize = function (x) {
private$x = x
}
get = function () private$x
},
private = {
@klmr
klmr / config
Created October 20, 2023 09:45
Alternative R linters
linters:
config_dir = dirname(parent.frame(3L)$config_file)
lapply(dir(config_dir, '\\.r$', full.names = TRUE), source, local = environment())
lintr::linters_with_defaults(
assignment_linter = NULL,
function_left_parentheses_linter = NULL,
line_length_linter(120L),
object_usage_linter = NULL, # unusably buggy
lintr::quotes_linter("'")
)
@klmr
klmr / doc-api.r
Last active June 15, 2022 12:59
Version of “Nested function structure” from https://coolbutuseless.github.io/2022/06/15/nesting-functions/ without NSE
doc = function (...) {
doc = new.env(parent = emptyenv())
doc$words = lapply(list(...), add_node, doc = doc)
doc
}
par = function (...) {
structure(list(...), class = c("par", "docnode"))
}
box::use(
dplyr[...],
glue[glue],
grid,
ld = lubridate,
ragg[...],
rvest,
scales[label_date_short, label_percent],
tidyr[...],
./screen_size[...],
@klmr
klmr / solution.r
Created December 17, 2020 16:11
Advent of Code day 15 in R
test_cases_desc = R'(
Given the starting numbers 1,3,2, the 2020th number spoken is 1.
Given the starting numbers 2,1,3, the 2020th number spoken is 10.
Given the starting numbers 1,2,3, the 2020th number spoken is 27.
Given the starting numbers 2,3,1, the 2020th number spoken is 78.
Given the starting numbers 3,2,1, the 2020th number spoken is 438.
Given the starting numbers 3,1,2, the 2020th number spoken is 1836.
)'
`%>%` = magrittr::`%>%`
import pathlib
p = pathlib.Path('/foo')
print(f'p: "{p}", parent: "{p.parent}", root: "{p.root}"')
# p: "/foo", parent: "/", root: "/"
print(f'parent == root: {p.parent == p.root}')
# parent == root: False
print(f'parent.samefile(root): {p.parent.samefile(p.root)}')
# parent.samefile(root): True
We couldn’t find that file to show.
@klmr
klmr / simple.r
Last active April 22, 2020 09:47
Generate N random numbers fulfilling some criteria; see https://stackoverflow.com/q/61352556/1968
alpha = numeric(1e4L)
beta = numeric(1e4L)
i = 0L
while (i < 1e4L) {
a = rnorm(1L, 10, 2)
b = rgamma(1L, 8, 1)
d = a - b
if (d < 1) {
i = i + 1L
alpha[i] = a
@klmr
klmr / rng.cpp
Last active November 3, 2020 21:20
Correctly seed an arbitrary RNG in C++
#include <algorithm>
#include <array>
#include <functional>
#include <random>
template <typename T = std::mt19937>
auto get_random_generator() -> T {
auto constexpr seed_bytes = sizeof(typename T::result_type) * T::state_size;
auto constexpr seed_len = seed_bytes / sizeof(std::seed_seq::result_type);
auto seed = std::array<std::seed_seq::result_type, seed_len>();
@klmr
klmr / read_file.cpp
Last active April 7, 2021 20:29
“Canonical” code to slurp a file in C++17
auto read_file(std::string_view path) -> std::string {
constexpr auto read_size = std::size_t{4096};
auto stream = std::ifstream{path.data()};
stream.exceptions(std::ios_base::badbit);
auto out = std::string{};
auto buf = std::string(read_size, '\0');
while (stream.read(& buf[0], read_size)) {
out.append(buf, 0, stream.gcount());
}