This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### ----------------------------------------------------------------- ### | |
### SET UP THE PROBS ---- | |
### ----------------------------------------------------------------- ### | |
roll_dice <- function() { | |
dice <- sample(seq(100L), 1L) | |
if (dice <= 51L) { | |
FALSE | |
} else if (dice > 51L) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "RMarkdown knit_hooks" | |
date: "`r Sys.Date()`" | |
output: | |
html_document: | |
highlight: textmate | |
--- | |
```{r setup, include=FALSE} | |
## Source: https://stackoverflow.com/a/39025054 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table <- knitr::kable(mtcars, "html") | |
tmp_path <- tempfile() | |
html_tmp_path <- paste0(tmp_path, ".html") | |
## styles are from: Duckett, J. (2013) HTML & CSS p. 338 | |
styles <- ' | |
body { | |
font-family: Arial, Verdana, sans-serif; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gdp <- data.frame( | |
"Luxembourg" = c(90661, 92969, 92102, 94823, 99738, 101255, 103286, 106373, 110870, 112622), | |
"Ireland" = c(43291, 45359, 46058, 47422, 52133, 65656, 69248, 75538, 79925, 81686), | |
"Norway" = c(61601, 62656, 64699, 65673, 67377, 68795, 69807, 71830, 74065, 76620), | |
"Switzerland" = c(53263, 54769, 55728, 57098, 58808, 59423, 60365, 61421, 63380, 66780), | |
"Netherlands" = c(44839, 46309, 46491, 47015, 48363, 49780, 51248, 53634, 56435, 59105), | |
"Iceland" = c(38594, 40022, 41005, 42953, 44220, 46146, 49683, 51841, 54121, 56915), | |
"Germany" = c(40850, 43249, 44266, 45127, 46627, 47429, 48532, 50425, 52801, 54984), | |
"Denmark" = c(41957, 43193, 43933, 44882, 46223, 47202, 48338, 49883, 51643, 54564), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## The birthday paradox is very famous in probability. If you take 23 people, | |
## there's about a 50/50 chance that two of them share a birthday. With 50 | |
## people, it's a 97% chance. | |
## Source: https://twitter.com/3blue1brown/status/1192570973439946754 | |
library(ggplot2) | |
birthday <- function(n, iterations) { | |
res <- vapply(seq(iterations), function(.) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Create language elements in R at C level | |
## Create class with Rf_lang | |
inline::cfunction(c(x = "numeric"), ' | |
SEXP class_call = PROTECT(Rf_lang2(R_ClassSymbol, x)); | |
UNPROTECT(1); | |
return class_call; | |
') -> class_expr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline::cfunction(c(asexp = "integer", bsexp = "integer"), ' | |
int a = Rf_asInteger(asexp); | |
int b = Rf_asInteger(bsexp); | |
int avg = (a>>1)+(b>>1)+(a&b&1); | |
SEXP out = PROTECT(allocVector(INTSXP, 1)); | |
out = Rf_ScalarInteger(avg); | |
UNPROTECT(1); | |
return out; | |
') -> avg_two_ints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(gh) | |
## NOTE: | |
## Don't forget to introduce token not to be constrained by rate limit of GH. | |
## Sys.setenv(GITHUB_TOKEN = "blabla") | |
## (ask more page than it needs and then eliminate the empty ones in the second step) | |
n_pages <- 10L |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Verdict: memset doesn't have a clear performance advantage and it has | |
## restrictive use. | |
## Pointer is important to use. | |
## Resources: | |
## http://adv-r.had.co.nz/C-interface.html | |
library(inline) | |
library(microbenchmark) | |
library(profmem) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_git_logs <- function(https) { | |
## temporarily set locale to EN/US: | |
old_locale <- Sys.getlocale("LC_TIME") | |
Sys.setlocale("LC_TIME", "en_US.UTF-8") | |
on.exit(Sys.setlocale("LC_TIME", old_locale), add = TRUE) | |
hash <- paste(sample(c(seq(1, 49), letters, LETTERS), 5), collapse = "") | |
path <- file.path("var", "tmp", hash) |