Skip to content

Instantly share code, notes, and snippets.

View bakaburg1's full-sized avatar

Angelo D'Ambrosio bakaburg1

  • European Centre for Disease Prevention and Control
  • Stockholm, Sweden
  • X @Bakaburg1
View GitHub Profile
@bakaburg1
bakaburg1 / figure_caption_filter.lua
Last active February 28, 2022 15:24
A filter to store image captions into the Meta fields
function RawBlock (raw)
local caption = raw.text:match("\\caption%{[^%}]+%}")
if caption then
captions:insert(caption)
end
end
function Pandoc (doc)
-- append collected captions at the end
doc.meta.figures = captions -- This works but latex is escaped by \textbackslash
@bakaburg1
bakaburg1 / gist:817be0e276d94032f1b654afb34452bb
Last active December 27, 2021 19:26
Scrape unbound globals which creates warnings with R CMD CHECK
library(stringr)
library(dplyr)
check_logfile <- ...
readLines(check_logfile) %>% paste(collapse = '\n') %>%
str_extract_all("[\\w_]+ ?:( <anonymous>:)?[\\s\\n]+no[\\s\\n]+visible[\\s\\n]+binding[\\s\\n]+for[\\s\\n]+global[\\s\\n]+variable[\\s\\n]+‘.*’") %>%
lapply(function(x) {
fun <- str_extract(x, '^[\\w_]+')
@bakaburg1
bakaburg1 / app.R
Created November 2, 2021 20:26
Child Julia process from shiny app do not survive parent process
library(shiny)
print(paste('Inline Julia pID:', sys::exec_background('nohup', c("julia", "-e sleep(3000)"))))
print(paste('Julia script pID:', sys::exec_background('nohup', c("julia", "test.jl", "&"))))
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
@bakaburg1
bakaburg1 / ctree_rules.R
Last active October 1, 2020 23:18
Small helper functions to interact with partykit::ctree().
#' Extract ctree rules.
#'
#' Extract the tree rules, optionally formatting them in order to be ready to
#' use for data filtering.
#'
#' @param tree An object produced by \code{partikit::ctree()}.
#' @param rule.as.text Whether to collapse rule conditions into a string or
#' leave them as a character vector.
#' @param eval.ready Whether to format rules in order to be easily eval() for
#' data filtering. Turns \code{rule.as.text} automatically on.
brms.mod.nprest.nb <<- brm(bf(N.prest ~ Mesi + (Mesi|Context), family = negbinomial()), data = TdA, prior = c(
set_prior('normal(0, 2.5)', class = 'Intercept'),
set_prior("cauchy(0, 2.5)", class = 'b')#,
#set_prior("normal(0, 2.5)", class = 'sd')
), init_r = 0.1, cores = 8, chains = 8, iter = 8000, warmup = 2000, control = list(max_treedepth = 15, adapt_delta = .90))
brms.mod.attesa.prior <- brm(bf(Attesa ~ log(N.prest) + (log(N.prest)|Context), shape ~ log(N.prest) + (log(N.prest)|Context), family = Gamma('log')), data = TdA, prior = c(
set_prior('normal(0, 2.5)', class = 'Intercept'),
set_prior("cauchy(0, 2.5)", class = 'b'),
set_prior("normal(0, 1.5)", class = 'sd', group = 'Context'),
set_prior("normal(0, 1.5)", class = 'sd'),
set_prior("student_t(3, 0, 2.5)", class = 'sd', dpar = 'shape'),
set_prior("student_t(3, 0, 2.5)", class = 'sd', group = 'Context', dpar = 'shape')
), init_r = 0.1, cores = 8, chains = 8, iter = 8000, warmup = 2000, control = list(max_treedepth = 15, adapt_delta = .90))
Error in stan_base_prior(prior) : length(base_prior) == 1L is not TRUE
@bakaburg1
bakaburg1 / mcdo.R
Last active March 17, 2019 12:24
Very simple parallelized implementation of dplyr::do() using mclapply.
library(parallel)
library(pbmcapply)
library(dplyr)
mcdo <- function(.data, ...) {
if (progress.bar) {
pkg.require(c('pbmcapply'))
pfun <- pbmclapply
} else pfun <- mclapply
@bakaburg1
bakaburg1 / gist:6345023
Last active December 21, 2015 18:08
Horizontal rule with text. #css #webdesign
<div style="
position: relative;
">
<span style="
position: absolute;
top: -900%;
left: 3%;
background: white;
padding: 0 10px;
">Blah Blah</span>