2

I'm looking to add a Bootstrap navigation bar (as shown here: https://getbootstrap.com/docs/4.0/components/navbar/) to an R Markdown template I am creating.

From reading around, I've seen how you can add HTML elements from another file and call them within R Markdown.

But, is there anyway to include the HTML code inside of the markdown file, that way that navigation bar still gets added, but I only have a single page needed to show the results?

Steveman30290
  • 511
  • 7
  • 17

2 Answers2

6

This requires a .yml(yaml) file to compliment our Rmarkdown file.

our almost empty Rmarkdown file titled index.Rmd...

---
title: "navbar for stackoverflow"
---

Now we just add code to our .yml file for our navbar. Rmarkdown looks inside it's root directory for a _site.yml file for rendering instructions per the blogdown book, which is the same author as your reference.

inside our _site.yml file inside the same directory as our index.Rmd file...

name: "Rmarkdown with navbar"                                
output_dir: "."
navbar:
  title: "Rmarkdown with navbar"                            
  type: inverse
  right:
    - text: "Contact me"
      icon: fa-envelope-o
      href: https://www.stackoverflow.com                  
    - text: "GitHub"
      icon: fa-github
      href: https://www.stackoverflow.com  
    - text: "Stackoverflow"
      icon: fa-stack-overflow
      href: https://www.stackoverflow.com  
    - text: "Youtube"
      icon: fa-youtube
      href: https://www.stackoverflow.com  
    - text: "Instagram"
      icon: fa-instagram
      href: https://www.stackoverflow.com  
    - text: "Twitter"
      icon: fa-twitter
      href: https://www.stackoverflow.com  
output:
  html_document:
    theme: spacelab
    highlight: textmate

Which renders the below output.

enter image description here

The theme argument in the yaml file IS one of the select few bootstrap options Rmarkdown comes installed with.

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • How can I add the scroll to a section animations when clicking on a button? – lmngn23 Apr 25 '21 at 02:36
  • I think that would be called “anchors” so look up “rmarkdown anchors” or [here is a link](https://stackoverflow.com/questions/5319754/cross-reference-named-anchor-in-markdown) for a similar SO question – Daniel_j_iii Apr 25 '21 at 10:30
1

Figured this out! The best solution I have found for this is to use RHTML to create the markdown file:

https://bookdown.org/yihui/rmarkdown-cookbook/html-hardcore.html

From there, you can use HTML to create the backbone of your document, and wrap your R code like so:

<!--begin.rcode
df <- data.frarme(x = 1:5, y = 1:5)
df
end.rcode-->

This definitely will require more work on my end, but it gives me exactly what I've been looking for.

Steveman30290
  • 511
  • 7
  • 17