Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
artemklevtsov / .gitlab-ci.yml
Last active January 12, 2024 04:14
Testing R package with GitLab CI (with code coverage)
variables:
CODECOV_TOKEN: "CODECOV_TOKEN_STRING"
_R_CHECK_CRAN_INCOMING_: "false"
_R_CHECK_FORCE_SUGGESTS_: "true"
APT_PKGS: "libcurl4-openssl-dev libssh2-1-dev libssl-dev libxml2-dev zlib1g-dev git"
before_script:
- apt-get update
- apt-get install -y --no-install-recommends ${APT_PKGS}
- apt-get install -y --no-install-recommends qpdf pandoc pandoc-citeproc
@artemklevtsov
artemklevtsov / enum.go
Created May 26, 2022 18:33 — forked from lummie/enum.go
Golang Enum pattern that can be serialized to json
package enum_example
import (
"bytes"
"encoding/json"
)
// TaskState represents the state of task, moving through Created, Running then Finished or Errorred
type TaskState int
@artemklevtsov
artemklevtsov / radix.nim
Created February 21, 2022 16:40 — forked from treeform/radix.nim
Radix sort in Nim.
## Radix sort in O(n) unlike other sorting methods that can be way worse.
## But radix sort needs the bits to operate on, which makes it different then
## other sorting algorithms.
proc getMax(arr: seq[SomeInteger]): SomeInteger =
for i, e in arr:
if i == 0:
result = e
elif e > result:
result = e
@artemklevtsov
artemklevtsov / raw_char.cpp
Last active September 13, 2020 07:43
charToRaw and charToRaw Rcpp implementation.
// [[Rcpp::plugins(cpp17)]]
#include <Rcpp.h>
// [[Rcpp::export(rng = false)]]
Rcpp::RawVector Cpp_charToRaw1(const std::string& s) {
Rcpp::RawVector res(s.begin(), s.end());
return res;
}
// [[Rcpp::export(rng = false)]]
@artemklevtsov
artemklevtsov / PKGBUILD
Last active May 5, 2020 06:35
PKGUILBD for the fancon
# Maintainer: Artem Klevtsov <a.a.klevtsov@gmail com>
_pkgbase=fancon
pkgname=fancon-git
pkgver=0.20.1.r3.gbebdfc8
pkgrel=1
pkgdesc="A Linux fan control daemon"
arch=('x86_64')
url="https://github.com/hbriese/${_pkgbase}"
license=('Apache')

I tried searching for the data.table functions to trunc dates.

Bug in round.IDate(x, "week")

round.IDate for the weeks (IDateTime.R#81) seems inaccurate: first week of year have a 6 days instead 7 (fix: should be yday(x) - 1L)

> rle(unclass(round(as.IDate(0:21), "week")))
Run Length Encoding
 lengths: int [1:4] 6 7 7 2
@artemklevtsov
artemklevtsov / floor_date.R
Last active March 16, 2020 17:46
Floor dates function
#' @title Floor Dates
#' @param x A vector of date.
#' @param unit A character string specifying a time unit.
#' @param start.on.monday Should the week start on Mondays or Sundays?
#' @return An object of class "Date".
floor_date <- function(x, unit = c("day", "week", "month", "quarter", "year"), start.on.monday = TRUE) {
stopifnot(is(x, "Date"))
unit <- match.arg(unit)
if (unit == "day") {
return(x)
#' @title Перекодировка столбцов таблицы
#' @param x Вектор или таблица.
#' @param enc Строка, содержащая название кодировки. Если параметр не задан, используется автоопределение кодировки.
#' @param defqult Строка, содержащая кодировку по умолчанию, которая будет использована в случае, если автоматически определить кодировку не удалось.
#' @return Исходный объект, с перекодированными элементами.
#' @details
#' Функция изменяет объект на месте, то есть будет изменён переданный объект.
#' Для определения кодировки используется пакет `Ruchardet`.
#' Предполагается, что вектор (столбец) содердит только одну кодировку.
#' @importFrom data.table set setDT
@artemklevtsov
artemklevtsov / utf8_to_int.cpp
Last active October 15, 2019 15:48
utf8ToInt Rcpp implementation
// [[Rcpp::plugins(cpp17)]]
#include <Rcpp.h>
#include <codecvt>
using namespace Rcpp;
// [[Rcpp::export(rng=false)]]
std::vector<unsigned long> utf8_to_int(const std::string& x) {
std::size_t n = x.size();
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export(rng = false)]]
List df2list(const DataFrame& x) {
std::size_t nrows = x.rows();
std::size_t ncols = x.cols();
CharacterVector nms = x.names();
List res(no_init(nrows));
for (std::size_t i = 0; i < nrows; ++i) {