Hi,
I am developing a package that uses some js code in a RMarkdown document, or more precisely, in the html output of it. My problem is that I don't know how to ingest the javascript through a package function...
What I mean by that is the following: for a Shiny app, I can do it via a function like:
ingest_dependency <- function(){
scripts_list <- c("js/script.js")
htmltools::htmlDependency(
name = "mypackage",
version = utils::packageVersion("mypackage"),
package = "mypackage",
src = ".",
script = scripts_list
)
}
Then it is enough to call it from the ui and inst/js/script.js from the package is available. How can I do something similar in js (using a package function)?
I think you'll want to create a new output format function. The chapter Creating New Formats from the book R Markdown: The Definitive Guide provides some basic examples, but doesn't go so far as adding custom JS dependencies.
For a more realistic example, check out the function gitbook() from bookdown. It extends the output format rmarkdown::html_document() by passing its dependencies to the argument extra_dependencies (source). The dependencies are actually defined in a separate function: gitbook_dependency(). And that function adds the CSS/JS files in inst/resources/gitbook/.
So the main steps are something along the lines of:
- Save your supporting CSS/JS files somewhere in the
inst/ directory of your package (the exact name doesn't matter)
- Use
system.file() to pass your package's supporting files (using the name of the directory you chose in the previous step) to htmltools::htmlDependency()
- Create a custom output format function that calls an existing output format function (e.g.
rmarkdown::html_document()) or starts a brand new one with rmarkdown::output_format(), passing the dependencies in the previous step to the argument extra_dependencies
Hi @jdblischak
Thanks for the detailed answer! I am not that familiar with Rmd options, but it does sound very similar to what I wanted.
I will take a look on the references. Thanks again!