Joe Parzival Joe Parzival - 1 year ago 153
HTML Question

Use internal links in RMarkdown HTML output

I am using R Studio to create a markdown document.
I tried:

Jump to [Header 1](#anchor)

I would like to set up a link so when a reader clicks it, can jump to a specific point on the page.

Let's say the point I want them to be directed to has a header "##Test".

Answer Source

Here is a solution for HTML documents using jQuery:

title: "Internal Links"
output: html_document

# First Section

## Second Section

### Third Section

<script type="text/javascript">
  // When the document is fully rendered...
  $(document).ready(function() {
    // all header elements...
    $('h1, h2, h3, h4, h5').each(function() {
      // ...and add an id to them corresponding to their 'titles'
      $(this).attr('id', $(this).html());

<a href="#First Section">Go to first section</a><br>
<a href="#Second Section">Go to second section</a><br>
<a href="#Third Section">Go to third section</a>

As the comments point out, we simply select all headers, read out their content (e.g. "First Section") and add the attribute id with the value corresponding to the specific content to each header. Now you can link to any header using #HEADER (e.g. #First Section).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download