set_widget_hooks: Set knitr hooks for htmlwidgets with Jekyll/GitHub Pages

View source: R/autolabel_latex_captions.R

set_widget_hooksR Documentation

Set knitr hooks for htmlwidgets with Jekyll/GitHub Pages

Description

Modified from my gist: https://gist.github.com/burchill/8392b2a753652e24a35a8a1dd707c1b1. This functions sets a knitr hook so that any HTML widgets that were printed (i.e., objects that inherit the 'htmlwidget', like those from the htmlwidgets or plotly packages) will work with a Jekyll system, like the one used for GitHub Pages.

Usage

set_widget_hooks(
  dep_dir = knitr::opts_chunk$get("plotly.savepath"),
  base_path = knitr::opts_chunk$get("proj.basedir"),
  hrefFilter = function(x) paste0("/", x)
)

Arguments

dep_dir

The directory you want to save the dependencies to.

base_path

The directory you want to make the dependency links relative to. For example, if your post is at the url yoursite.com/this_post/, then you'll want to make base_path the home directory of your project.

hrefFilter

This function lets you perform any additional manipulations to the dependency links. For some reason, when I give my base directory as my project's home directory, the links come back without the necessary "/" in front of them. This function adds that to each link.

Details

It essentially sets a hook so that, when the document is finished being knitted, it moves all the dependencies necessary for the widgets to a directory, and then adds HTML code to the document to load those files from their new location. Additionally, it sets the default screenshot.force chunk option to FALSE, so knitr doesn't try to use a screenshot instead of the widget.

See https://www.zachburchill.ml/plotly_with_jekyll/ for background and (https://gist.github.com/burchill/9df0f6245ea7768e5b6bbd0a1c22db08) for the old, bad version of this script.


burchill/zplyr documentation built on Feb. 2, 2023, 11:01 a.m.