Skip to content

Instantly share code, notes, and snippets.

View traversc's full-sized avatar

Travers traversc

View GitHub Profile
total_time <- Sys.time()
suppressMessages(library(Rcpp))
suppressMessages(library(dplyr))
suppressMessages(library(data.table))
suppressMessages(library(qs))
suppressMessages(library(stringfish))
options(warn = 1)
do_gc <- function() {
@traversc
traversc / bench_stringi.R
Last active March 27, 2022 00:49
Benching stringi with and without altrep
library(stringi) # devtools::install_github("traversc/stringi")
library(tictoc)
enwik8 <- readLines("~/Downloads/enwik8", warn = F)
search <- c("Wikipedia", "^.")
replace <- c("Encyclopedia", "")
tic("WITHOUT alt rep")
stri_use_alt_rep(FALSE); result <- stri_replace_all_regex(enwik8, search, replace, vectorize_all = F)
toc()
@traversc
traversc / simple_progress.h
Last active September 16, 2021 05:34
Rcpp Simple Progress
#include <atomic>
#include <thread>
#include <Rcpp.h>
#include <R_ext/Print.h>
using namespace Rcpp;
class simple_progress {
private:
const size_t max;
std::atomic<size_t> counter;
library(qs)
library(data.table)
library(dplyr)
library(ggplot2)
library(fst)
library(patchwork)
library(ggformula)
library(hrbrthemes)
library(Rcpp)
library(trqwe)
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <array>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
@traversc
traversc / fdtest.cpp
Created July 24, 2019 21:02
Using file descriptors directly in R
#include <cerrno>
#include <fcntl.h>
#include <cstdio>
#include <unistd.h>
#include <string.h>
#include <array>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
@traversc
traversc / altrepstring.cpp
Last active January 15, 2019 17:50
alt rep string test
#include <iostream>
#include "altrepisode.h"
#include <string>
#include <vector>
std::string getRandomStrings(int seed, int len = 30) {
std::srand(static_cast<unsigned int>(seed));
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@traversc
traversc / fastAUC.r
Last active April 12, 2021 09:31
Fast AUC and ROC calculations in R
fastROC <- function(probs, class) {
class_sorted <- class[order(probs, decreasing=T)]
TPR <- cumsum(class_sorted) / sum(class)
FPR <- cumsum(class_sorted == 0) / sum(class == 0)
return(list(tpr=TPR, fpr=FPR))
}
# Helpful function adapted from: https://stat.ethz.ch/pipermail/r-help/2005-September/079872.html
fastAUC <- function(probs, class) {
x <- probs