Search code examples
bookdown

embedding youtube videos in bookdown


I tried using https://bookdown.org/yihui/rmarkdown/learnr-videos.html

![](https://youtu.be/jg91ikK0OCI)

but I get the error

! LaTeX Error: File `https://youtu.be/jg91ikK0OCI' not found.

Error: LaTeX failed to compile learn-isabelle.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See learn-isabelle.log for more info.
Execution halted
Error: bookdown::render_book() failed to render the output format 'bookdown::pdf_book'.
Execution halted

I don't really care about pdf anyway. I just want HTML version of book. I heard learnr requires shiny backend https://github.com/rstudio/learnr/issues/168 . I don't even care about learnr. I just want to embed youtube videos in HTML output of bookdown.

I removed everything from _output.yaml. Previously this is what I had there

bookdown::gitbook:
  css: style.css
  config:
    toc:
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
    edit: https://github.com/rstudio/bookdown-demo/edit/master/%s
    download: ["pdf", "epub"]
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
bookdown::epub_book: default
bookdown::bs4_book: default

Removing it, prevented pdf from being generated and the error is gone but the youtube embedding still doesn't work.


Solution

  • The solution turned out to be quite obvious. Just embed the youtube video using iframe like so

    <iframe width="560" height="315" src="https://www.youtube.com/embed/jg91ikK0OCI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>