richarddmorey / encryptRmd

R package to encrypt chunk outputs in HTML compiled from Rmd documents

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

encryptRmd

R package to encrypt chunk outputs in HTML compiled from Rmd documents

Installation

install.packages('remotes')
remotes::install_github('richarddmorey/encryptRmd')

Usage

See the example.

  1. Create an Rmarkdown document compatible with the output format rmarkdown::html_document
  2. In the chunk options for the chunks whose outputs you intend to encrypt, add the output_encrypt option set equal to the key, e.g.:
```{r output_encrypt = 'super_secret_key'}
# chunk code
```
  1. Change the output format in the header to encryptRmd::html_document_enc, e.g.:
---
title: "Encrypted document test"
author: "Richard D. Morey"
date: "`r Sys.Date()`"
output: encryptRmd::html_document_enc
---
  1. Compile the document.

Decryption

The resulting document will have elements that you can click to decrypt:


Compiled HTML document with encrypted elements

Clicking an encrypted element will bring up a box to enter a key:


Open window to enter key

When you enter a key, all elements encrypted with that key will be decrypted and displayed.


Page showing the first element decrypted

Advanced usage

Will only encrypt elements that are handled by the output and plot knitr hooks. If you'd like to encrypt arbitrary content, you can use an results='asis' chunk and call the encryption function encryptRmd::encrypt_content_html.

If you'd like to use javascript to modify or apply rendering functions to the content after decryption, you can add a listener in the page for the event 'decrypted':

window.addEventListener('decrypted', function(e){
  const el = e.target;
  /* do stuff with el */
});

For an example, see the function that renders markdown and MathJax in inst/includes/encrypted_chunk.js.

Quarto (and non-html_document Rmds)

It is possible to use this package with other kinds of html documents, if you include the right chunks that inject the dependencies and explicitly set the knitr output hooks. An example can be found in the examples directory.

Warning: Quarto seems to put the chunk options in the HTML source, so the keys are easily findable if you use the source, output or plot hooks. It is very important that you include a lua filter to remove the keys. See the example linked above; the lua filter is in the same folder.

About

R package to encrypt chunk outputs in HTML compiled from Rmd documents

License:MIT License


Languages

Language:R 87.9%Language:JavaScript 6.1%Language:CSS 4.9%Language:HTML 0.7%Language:Lua 0.4%