Closed gvelasq closed 8 months ago
The difference in the two scripts, namely
<script>!function(){\"use strict\"; ...
vs
<script defer data-domain=\"ivelasq.github.io/rladies-video-feed\" ...
is the result of pandoc's --embed-resource
flag (docs). This feature is enabled by default in most rmarkdown settings via the self_contained = TRUE
argument (or self_contained: true
yaml). When this is turned on, pandoc embeds all CSS and JavaScript into the final .html
output file so that you can share or deploy a single file rather than a directory of files.
You can turn this off for this specific script by adding data-external="1"
to the <script>
tag, e.g. <script defer data-external=1 data-domain="..." ...>
, or you can turn it off globally for the flexdashboard document:
---
title: ""
output:
flexdashboard::flex_dashboard:
self_contained: false
includes:
in_header: plausible.html
---
Thanks very much, @gadenbuie. I had tried self_contained: false
which had worked but had also removed many of the desirable features of the dashboard. The data-external=1
fix works great (reprex below)! Thanks for pointing it out in the Pandoc documentation.
library(flexdashboard)
library(glue)
suppressPackageStartupMessages(library(rmarkdown))
library(usethis)
find_script <- function(path) {
grep("plausible", readLines(path))
}
dom <- "ivelasq.github.io/rladies-video-feed"
src <- "https://plausible.io/js/script.js"
script <- glue('<script defer data-external=1 data-domain="{dom}" src="{src}"></script>')
cat(script, file = "./plausible.html", sep = "\n")
yaml_part1 <- '---\ntitle: "reprex1"\noutput:\n flexdashboard::flex_dashboard:'
yaml_part2 <- ' includes:\n in_header: plausible.html\n---'
cat(yaml_part1, yaml_part2, file = "./reprex.Rmd", sep = "\n")
render("./reprex.Rmd")
#> processing file: reprex.Rmd
#> output file: reprex.knit.md
#> /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc +RTS -K512m -RTS reprex.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output reprex.html --lua-filter /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --variable bs3=TRUE --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/flexdashboard/www/flex_dashboard/default.html --variable theme=cosmo --mathjax --variable 'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in-header /var/folders/qf/yt27gctx33x4pvc255t0w9zc0000gp/T//RtmpIqDwWY/rmarkdown-str7324a6142e3.html --highlight-style pygments --include-in-header plausible.html --include-in-header /var/folders/qf/yt27gctx33x4pvc255t0w9zc0000gp/T//RtmpIqDwWY/file732376e6b3bhtml --include-before-body /var/folders/qf/yt27gctx33x4pvc255t0w9zc0000gp/T//RtmpIqDwWY/file73232420aeb.html --include-after-body /var/folders/qf/yt27gctx33x4pvc255t0w9zc0000gp/T//RtmpIqDwWY/file732ddae414.html
#>
#> Output created: reprex.html
loc <- "./reprex.html"
find_script(loc) # line number in reprex.html
#> [1] 137
out <- scan(loc, character(), n = 1, sep = "\n", skip = find_script(loc) - 1)
out
#> [1] "<script defer data-external=\"1\" data-domain=\"ivelasq.github.io/rladies-video-feed\" src=\"https://plausible.io/js/script.js\"></script>"
Created on 2024-03-09 with reprex v2.1.0
Thank you for the great package; flexdashboard powers an R-Ladies YouTube dashboard that @ivelasq and I created and featured in her blog. We recently added Plausible analytics to the dashboard and noticed that the answer to a related issue provided here doesn't work for Plausible:
The reason is because Plausible requires their analytics script to be added to the header verbatim, however during the
rmarkdown::render()
process a malformed script is added to the header when using the YAML above.The reprexes below show the following:
includes:
followed byin_header:
: a malformed script is added to the header.pandoc_args:
followed byinclude-in-header:
: a malformed script is added to the body. It's unclear to me why this adds the script so far down the page.sed
. This could be implemented in flexdashboard in a way that retains the old behavior when files are passed toincludes:
, by adding an option for direct text input that interpolates text verbatim into the rendered .html (see 4. below).Created on 2024-03-07 with reprex v2.1.0