Skip to content

Instantly share code, notes, and snippets.

View RLesur's full-sized avatar

Romain Lesur RLesur

View GitHub Profile
@RLesur
RLesur / pdfmarks
Created May 3, 2020 20:20 — forked from shreve/pdfmarks
Example of adding PDF metadata (bookmarks / ToC) with Ghostscript
[ /Title (Elementary Differential Equations)
/Author (Edwards & Penney)
/DOCINFO pdfmark
[ /Title (Contents)
/Page 6
/OUT pdfmark
[ /Count -8
/Title (1. First-Order Differential Equations)
@RLesur
RLesur / crrry.R
Last active November 18, 2018 16:27
A proposal for a Chrome Remote Interface with R
# A proposal for a Chrome Remote Interface with R
# Copyright: Romain Lesur
# Year: 2018
# License: MIT
# Browser and websocket connection helpers --------------------------------
# From milesmcbain/chradle (MIT License)
debugger_200_ok <- function(port, retry_delay = 0.2, max_attempts = 15, attempt = 1) {
if (max_attempts <= attempt) {
stop(paste0("Reached max attempts (", max_attempts,
@RLesur
RLesur / pdf_ws.R
Created November 14, 2018 22:47
PDF generation from R using Chrome and rstudio/websocket
# Packages ---------------------------------------------------------------
remotes::install_github('milesmcbain/chradle')
remotes::install_github('rlesur/pagedown@automation')
remotes::install_github('rstudio/websocket')
library(chradle)
library(pagedown)
library(websocket)
library(jsonlite)
# Chrome headless helpers -------------------------------------------------
@RLesur
RLesur / pdf.R
Last active November 14, 2018 10:22
PDF generation from R using the Chrome DevTools Protocol
# Packages ---------------------------------------------------------------
remotes::install_github('milesmcbain/chradle')
remotes::install_github('rlesur/pagedown@automation')
library(chradle)
library(pagedown)
library(httpuv)
# Chrome headless helpers -------------------------------------------------
# Thanks to Miles McBain
browser_init <- function(debug_port = 9222, bin = "google-chrome"){
---
title: Glossary
output:
bookdown::pdf_document2:
template: two-col.latex
latex_engine: xelatex
number_sections: FALSE
toc: False
pandoc_args: ["--lua-filter", "sort_definition_list.lua"]
includes:
@RLesur
RLesur / sort_definition_list.lua
Created October 27, 2018 23:22
A pandoc lua filter that sorts definition lists
DefinitionList = function(dl)
local terms = {}
local inlines = {}
local blocks = {}
local sorted = {}
for i, item in ipairs(dl.content) do
local term = string.upper(pandoc.utils.stringify(item[1])) --string.upper() is used because all the terms were not capitalized in the example
table.insert(terms, term)
inlines[term] = item[1]
blocks[term] = item[2]
@RLesur
RLesur / pagedjs_template.html
Last active September 4, 2018 23:50
R Markdown HTML4 template with paged.js - Use `theme=NULL`, `mathjax=NULL`, choose another pandoc math engine and open with Chrome
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"$if(lang)$ lang="$lang$" xml:lang="$lang$"$endif$>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
@RLesur
RLesur / google_fonts.md
Created June 30, 2018 21:38 — forked from cvan/google_fonts.md
get ttf, woff, woff2 from Google Fonts

ttf

curl 'https://fonts.googleapis.com/css?family=Karla'

woff2

curl 'https://fonts.googleapis.com/css?family=Karla' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'User-Agent: AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116'

woff

@RLesur
RLesur / case_when_factory.R
Last active October 31, 2023 23:48
How to create reusable dplyr::case_when() functions?
# case_when factory: ----------------------------------------------
create_case_when <- function(..., vars = "x") {
fun_fmls <- purrr::map(rlang::set_names(vars), ~ rlang::missing_arg())
fun_body <- substitute({
for (name in var) {
symb <- rlang::eval_bare(rlang::sym(name))
var <- rlang::eval_tidy(rlang::enquo(symb))
assign(name, var)
}
forms <- purrr::map(formulas, rlang::`f_env<-`, value = environment())