Skip to content

Instantly share code, notes, and snippets.

Avatar

Davis Vaughan DavisVaughan

View GitHub Profile
@DavisVaughan
DavisVaughan / .R
Created Nov 3, 2020
tsibble-to-slider.R
View .R
x <- c(1, 2, 3, 4, 5)
# partial = FALSE -> only "complete" windows are allowed
#
# `size = 2, align = right` translates to:
# "the current value, and one before it"
#
tsibble::slide(x, identity, .size = 2, .partial = FALSE, .align = "right")
#> [[1]]
#> [1] NA
View slider-even-years.R
library(slider)
library(dplyr)
library(lubridate)
library(tidyr)
x <- as.Date(c(paste0(2008:2014, "-01-01"), paste0(2008:2014, "-01-02")))
x <- sort(x)
df <- data.frame(x = x)
df <- mutate(df, year = year(x))
View future-slider.R
future_slide <- function(.x,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE) {
size <- vctrs::vec_size(.x)
names <- vctrs::vec_names(.x)
View calling-dt-parallel.R
fn <- function() {
FUN <- function(x) {
data.table::getDTthreads()
}
parallel::mclapply(1:2, FUN, mc.cores = 2)
}
# Call `getDTthreads()` from the workers
# Should return 1 because we forked
fn()
View fn-env.R
library(rlang)
#> Warning: package 'rlang' was built under R version 3.6.2
generator <- function() {
# ~ 80mb
x <- 1:1e7 + 0
# `x` is in this function env
function() {
1
View progressr.R
library(future)
library(progressr)
library(furrr)
# 2 cores
plan(multisession, workers = 2L)
x <- 1:20
random_sleep <- function() {
View testing-future-cleanup.R
library(future)
# //////////////////////////////////////////////////////////////////////////////
# Fresh R session takes around 40MB
pryr::mem_used()
#> Registered S3 method overwritten by 'pryr':
#> method from
#> print.bytes Rcpp
#> 35.7 MB
View tidy-rolling-regression.r
library(dplyr)
library(slider)
library(lubridate)
library(tsibbledata)
# Google, Apple, Facebook, Amazon stock
gafa_stock <- as_tibble(gafa_stock)
gafa_stock <- select(gafa_stock, Symbol, Date, Close, Volume)
head(gafa_stock, 2)
View gist:d7267a01191295daa75b588c76562e55
dtrace: system integrity protection is on, some features will not be available
PID/THRD SYSCALL(args) = return
dtrace: 12911 dynamic variable drops with non-empty dirty list
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
dtrace: error on enabled probe ID 2170 (ID 172: syscall::open:return): invalid user access in action #5 at DIF offset 0
You can’t perform that action at this time.