Skip to content

Instantly share code, notes, and snippets.

@jdblischak
Created February 28, 2025 20:18
Show Gist options
  • Save jdblischak/5698a425f326f59c49a739d0732eb06c to your computer and use it in GitHub Desktop.
Save jdblischak/5698a425f326f59c49a739d0732eb06c to your computer and use it in GitHub Desktop.
A minimal example that uses a knitr output hook to redact long alphanumeric strings. Motivated by https://fosstodon.org/@sckottie/114078121814425933
---
title: "Untitled"
output: html_document
date: "2025-02-28"
---
```{r setup, include=FALSE}
# knitr output hook to redact alphanumeric strings
#
# Adapted from
# https://bookdown.org/yihui/rmarkdown-cookbook/hook-truncate.html
# save the built-in output hook
hook_output <- knitr::knit_hooks$get("output")
# set a new output hook to truncate text output
knitr::knit_hooks$set(output = function(x, options) {
redacted <- gsub("[a-z,A-Z,0-9]{50,}", "**REDACTED**", x)
hook_output(redacted, options)
})
```
```{r make-token}
make_token <- function(len = 50) {
token <- sample(c(0:9, letters, LETTERS), size = len, replace = TRUE)
paste(token, collapse = "")
}
```
```{r token-to-redact}
make_token()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment