Created
February 28, 2025 20:18
-
-
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
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: "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