Automatically collapsing sidebar of R Bookdown website - r

It is critical that people reading my Bookdown project do not skip chapters. While it is explicitly stated in the document, further discouraging this by automatically collapsing the sidebar would be really helpful :o)
According to this Github issue there is no default explicit option to do this, but is there any other way?

Welcome to stackoverflow!
Using e.g. the inspect feature of Chrome on a bookdown::gitbook you see that the first element in the DOM after <body> is a <div> which 'contains' the whole book. This <div> has multiple classes, one of them is with-summary and this is the one you'd like to remove.
I think the quickest way to do this is jusing jquery:
Set up an HTML file
header.html
<script>
window.addEventListener("DOMContentLoaded", function(){
$("div").first().removeClass("with-summary");
});
</script>
Include the file in the document header using the YAML option includes in your
.Rmd
---
title: "My Title"
output:
bookdown::gitbook:
includes:
in_header: header.html
---
After the page load, the jquery function will pick the first <div> element in the DOM and remove the class. Note that this will hide the sidebar if the user navigates to another chapter, too.

Related

Change right sidebar header in bs4_book (bookdown)?

I'm using the bs4_book format to set up the framework for a new book project using bookdown. Is there a way to change "On this page" at the top of the right sidebar to "In this chapter"? So far, I haven't been able to find where that text snippet lives.
Here is a solution:
I placed a short JavaScript function in the file local_js.html in the folder with the Rmd chapter files. The contents are:
<script>
$(function() {
$("#toc h2").html("In this chapter");
});
</script>
(Using JQuery 3.5.1)
#toc is the id for the right sidebar contents list, and the heading text is inside the h2 tags.
Then I added the following to the YAML header for the b4_book output:
output:
bookdown::bs4_book:
theme:
primary: "#637238"
includes:
in_header: local_js.html
This seems to work well enough, at least for a book framework that still hasn't got much text or image content in the chapters. I still haven't figured out where the default heading "On this page" is coming from. If anyone knows and can explain how to change it directly, I'd be grateful.

Add code to Bookdown gitbook template before TOC

I'm creating a gitbook using bookdown, and the designer wants to put a banner at the top of the book, that is, across the entire page above both the TOC and the chapter text.
If I use the before_body attribute in the YAML, bookdown inserts the code after the TOC, so the banner is only at the top of the chapter. Gitbook doesn't seem to allow for the template: attribute.
Any suggestions for how I can squeeze a line of HTML right after the < body > tag to make my designer happy? :)
I managed to solve this myself--it turns out that although officially the gitbook theme doesn't allow the template: attribute, in practice it does. So I just grabbed the existing template, hard-coded my banner after the < body > tag, and referenced my new template in the YAML. I hope this is helpful for anybody else who runs into this problem!

How to force pandoc not to build <a> tag when url is in YAML metadata

Let's say we have simple empty .Rmd file with YAML metadata like this
---
link: https://google.com
---
Then in html template, it will show up automatically in <a> tag
http://google.com
How to force pandoc not to build <a> when there is url in YAML? I mean what I wanna finally get is plan text in html template
http://google.com
Or if I use own html template I can add a link that way
Google
Thanks for help!
This is caused by an extension that is enabled by default with the md_extensions option under the output format, specifically auto_link_bare_uris. You can disable this in your YAML header.
---
title: https://www.google.com
output:
html_document:
md_extensions: -autolink_bare_uris
---
Just note that this will affect auto linking of URIs throughout your document, not only in the title. After disabling the extension you will need to enclose links in angle brackets to create links, i.e. <https://www.google.com>. The Pandoc guide and the R Markdown site contain the detail covering this.
Replacing a character in the title with an HTML ASCII reference will stop the link from auto generating and display on the page correctly. For example, here I replace the colon:
---
title: https://gooogle.com
---

outline/toc on the left and contents on the right layout using rmarkdown

I was checking out how to include a nice table of contents / document outline in my rmarkdown document (with html output).
Well, the "standard" approach:
html_document:
toc: true
It works, but I do not like it (not even tweaking options and themes).
But actually, in several pages of the documentation (e.g. http://rmarkdown.rstudio.com/html_document_format.html), they use a very nice layout that includes a table of contents / document outline to the left of the page and the contents of the right (and the outline syncs with the contents part, Highlighting where on the contents you currenly are).
This is EXACTLY what I want. And I guess they did it using rmarkdown. But I cannot find how to do it.
I've check that documentation page, as well as rmarkdown gallery, flex dashboard, web sites among others. But I can't seem to figure it out how to do it.
Well, using web sites I would be able to do it, but it seems overly complicated for what I want (you need to have several pages, and at least index.Rmd file and the _site.yml file).
Instead, I guess there should be an option to simply tell rmarkdown to take my single document and put the section headers in a nice frame to the left, as in the example page from RStudio.
Have you tried this approach? It works for me and it's also on the homepage, you provided:
---
title: "Untitled"
output:
html_document:
toc: true
toc_float: true
---
# header 1
# header 2
# header 3
With a little bit of text, it looks like that:

How to change the position of the table of contents in rmarkdown?

With RStudio and knitr I see that I can add a TOC with the following code in my .rmd file.
----------------
output:
html_document:
toc: yes
-----------------
However, this places the TOC at the very beginning of the HTML document. Is there a way to move the TOC lower on the page? Say after an introductory paragraph?
I tried to use __TOC__ and __FORCETOC__ but it did not change the TOC position.
The position of the TOC is fixed in the R Markdown default HTML template. If you want to change its position in the document, you'll need to modify the template:
Make a copy of the R Markdown HTML template to use as a starting point. You can find it by running this R command: system.file("rmd/h/default.html", package="rmarkdown")
Move the $toc section to where you want the table of contents to appear.
Save the modified template in the same folder as the document you're rendering as e.g. lowertitle.html
Add template: lowertitle.html to the html_document settings.
From the standpoint of the template, all of the document's content is an atomic unit, so it might be necessary to put any content you want to appear before the TOC in the template itself.
You can use JQuery to relocate the TOC to an arbitrary position in the file. Simply insert a heading where you want the TOC to go, and use the ID generated by rendering the R Markdown file. For example:
<script>
// Move TOC to the Table of Contents heading (with id "table-of-contents")
$(function() {
$( "#TOC" ).insertAfter( $( "#table-of-contents" ) );
});
</script>
A heading called "Table of Contents" somewhere in the R Markdown file will receive id "table-of-contents". The TOC has id "TOC". The Jquery bit above selects that TOC, and inserts it after the "Table of contents" heading, wherever in the document it's located.
I tried the proposed answers and although they seem simple enough I could not achieve either. I found an easier solution proposed by #gadenbuie in GitHub:
https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8
You just need to copy and paste the function to render your TOC in your Rmarkdown file and then recall it in a chunk where you want your TOC to appear. Therefore, you can recall your TOC wherever you want it in the Rmarkdown file.
If you need further explanation, you can check out #gadenbuie's blog:
https://www.garrickadenbuie.com/blog/add-a-generated-table-of-contents-anywhere-in-rmarkdown/
You could add something in a header which will come after title and before toc, e.g. a yaml header like:
And then add whatever paragraph or figures to the file header.md that you want.
As an example, I set this YAML up in the README of brshallo/piececor so that I could have my 'lifecycle' badge show-up prior to my table of contents.

Resources