Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown Bootstrap Navbar

Tags:

r

r-markdown

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?

like image 608
Steveman30290 Avatar asked Sep 23 '26 19:09

Steveman30290


2 Answers

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.

like image 88
Daniel_j_iii Avatar answered Sep 25 '26 08:09

Daniel_j_iii


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.

like image 35
Steveman30290 Avatar answered Sep 25 '26 08:09

Steveman30290



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!