Quarto Book Download button - r

Using bookdown we are able to download different file formats from a download button, e.g.
---
bookdown::gitbook:
download:
- ["test.pdf", "PDF File"]
- ["test.html", "HTML File"]
- ["test.csv", "Data test.csv"]
---
In quarto, we only have the options for downloads: (one or more of pdf, epub, and docx). Has somebody an idea how to include data (e.g. .csv or .qs) via a download button to include on navbar or preferably sidebar?
Edit 1: After the great answer of #shafee, here is how my book at the moment looks like and where I would like to add the option to download the data.
By clicking on the downarrow, a dropdown menu opens and there I would like to add the option (optimal would be more than one, i.e. many datasets) "Download Data". Here is an example .yml:
---
project:
type: book
book:
title: "Quarto Book"
chapters:
- index.qmd
- intro.qmd
sidebar:
pinned: true
repo-url: https://www.rstudio.com/
search:
location: sidebar
type: textbox
downloads: [pdf, epub]
format:
html:
# include-after-body: custom.html
theme: cosmo
---
Edit 2: There could be a way using the tools option in the sidebar. Maybe someone has an idea how to correctly generate an URL for the data such that it will be downloaded directly.
sidebar:
pinned: true
tools:
- icon: save
menu:
- text: Data XLSX
url: ymlthis::yml_params_code(browseURL('data/mtcars.xlsx'))
- text: Data CSV
url: ymlthis::yml_params_code(browseURL('data/mtcars.csv'))

Updated Answer (Corresponding To OP's 2nd Edit)
The concept remains the same. Here we are just replacing those menu items under the tools with the button created from index.qmd file using javascript.
If you need to add more csv or xlsx data, just add the corresponding code for creating button inside the .download_btn pandoc divs and add more - text: "" and url: "#" in the _quarto.yml file.
_quarto.yml
project:
type: book
book:
title: "Quarto Book"
author: "Jane Doe"
date: "9/29/2022"
chapters:
- index.qmd
- intro.qmd
downloads: [pdf, epub]
sidebar:
pinned: true
tools:
- icon: save
menu:
- text: ""
url: "#"
- text: ""
url: "#"
- text: ""
url: "#"
repo-url: https://www.rstudio.com/
search:
location: sidebar
type: textbox
format:
html:
include-after-body: custom.html
theme: cosmo
index.qmd
# Preface {.unnumbered}
This is a Quarto book.
::: {.download_btn}
```{css}
#| echo: false
.btn-default,
.btn-default:hover,
.btn-default:active {
font-size: 20px;
color: black;
background-color: transparent;
border-color: transparent;
}
.btn-default:hover {
color: grey;
transition: 0.2s;
}
```
```{r}
#| echo: false
library(downloadthis)
mtcars %>%
download_this(
output_name = "mtcars data set",
output_extension = ".csv",
button_label = "mtcars.csv",
button_type = "default",
has_icon = TRUE,
icon = "fa fa-file-csv"
)
```
```{r}
#| echo: false
iris %>%
download_this(
output_name = "Iris data set",
output_extension = ".csv",
button_label = "iris.csv",
button_type = "default",
has_icon = TRUE,
icon = "fa fa-file-csv"
)
```
```{r}
#| echo: false
palmerpenguins::penguins %>%
download_this(
output_name = "penguins data set",
output_extension = ".csv",
button_label = "penguins.csv",
button_type = "default",
has_icon = TRUE,
icon = "fa fa-file-csv"
)
```
:::
custom.html
<script>
var all_btns = document.querySelectorAll(".download_btn a");
var data_nav = document.querySelectorAll('[aria-labelledby="sidebar-tool-dropdown-0"] li')
for (let i = 0; i < data_nav.length; i++) {
let li = document.createElement("li");
li.appendChild(all_btns[i]);
data_nav[i].parentElement.replaceChild(li, data_nav[i])
}
</script>
Old Answer
Since so far there's no default option from Quarto to add buttons for downloading csv data, as a workaround we can add a download button in the Book navbar with the help of {downloadthis} R-package and using a few line of javascript code.
So at first, add the code for creating button and a bit css to style the button's appearance in the index.qmd file with echo: false.
index.qmd
# Preface {.unnumbered}
This is a Quarto book.
::: {.download_btn}
```{css}
#| echo: false
.btn-default,
.btn-default:hover,
.btn-default:active {
font-size: 24px;
padding: 0px;
margin-top: -5px;
color: white;
background-color: transparent;
border-color: transparent;
}
.btn-default:hover {
color: whitesmoke;
transition: 0.2s;
}
```
```{r}
#| echo: false
library(downloadthis)
mtcars %>%
download_this(
output_name = "mtcars data set",
output_extension = ".csv",
button_label = "",
button_type = "default",
has_icon = TRUE,
icon = "fa fa-file-csv"
)
```
:::
Now by default, the button will be created in the book body of the index page. But we want this in the book navbar. To do that, we can add a save icon with href: "#" as a placeholder in the right side of the navbar, and then using javascript we can replace this save icon with our created download button.
Now to add js code, create an html file custom.html and put these lines of codes in that file and then add this custom.html file with include-after-body: custom.html in _quarto.yml.
custom.html
<script>
var btn = document.querySelector(".download_btn");
var nav = document.querySelector(".navbar-nav .bi-save");
nav.parentElement.replaceChild(btn, nav);
</script>
_quarto.yml
project:
type: book
book:
title: "Quarto Book"
chapters:
- index.qmd
- intro.qmd
navbar:
right:
- icon: save
href: "#"
format:
html:
include-after-body: custom.html
theme: cosmo
Now the rendered book navbar looks like,
Refer to the downloadthis package documentation to know about various options for adding buttons to download csv or xlsx files or any R-object as rds file and to know about how to style the buttons.
Disclaimer: I am just an R-user and know just a little bit javascript. So anyone, please feel free to edit this answer with more efficient js code. :-)

A solution without an external package works very similar to my 2nd edit (assuming that the data is stored in folder data:
sidebar:
pinned: true
tools:
- icon: save
menu:
- text: Data XLSX
url: "data/mtcars.xlsx"
- text: Data CSV
url: "data/mtcars.csv"

Related

Add a submenu to a distill rmarkdown website?

I am creating a distill website using rmarkdown and would like to add a submenu in the navigation bar. I found this feature request/issue on GitHub but have not been able to find a solution.
With the below _site.yml and index.Rmd the submenu text appears in the menu dropdown but is greyed-out and not clickable/selectable.
_site.yml:
name: "submenuTest"
title: "Submenu test"
description: |
Submenu test
output_dir: "_site"
navbar:
right:
- text: "Home"
href: index.html
- text: 'Menu'
menu:
- text: 'Page'
href: page.html
- text: 'Submenu'
menu:
- text: 'Sub Page'
href: subpage.html
output: distill::distill_article
index.Rmd:
---
title: "Submenu test"
description: |
Welcome to the website. I hope you enjoy it!
site: distill::distill_website
---
# here is my page content
In a plain rmarkdown .html (can use the above .yml and .Rmd examples but without the output: distill::distill_article and site: distill::distill_website lines) renders with a workable submenu.

How to make Rmarkdown website using one .rmd file as template for multiple pages instead of one .rmd for each page?

I find rmarkdown sites interesting, but I would like to change from most example I have seen and I haven't found something yet.
I try to make a repoducible example:
folder struture:
project_folder
\_ _site.yml
\_ index.rmd
\_ script.R
\_ templates
\_pages.rmd
I yml file alows to make the structure: I would like to have a website with the following categories (this an example of my _site.yml):
name: "website"
navbar:
title: "website"
left:
- text: "global info"
href: index.html
- text: "species"
menu:
- text: "all species result"
href: all.html
- text: "---------"
- text: "versicolor"
href: versicolor_1.html
- text: "setosa"
href: setosa.html
then an index.rmd (here just a title in order to keep the example simple).
---
title: Some stuff
---
a template page like page.rmd in a template folder (very simple as well but it uses a parameter.
---
params:
data_set:
title: "Region results"
---
hist(param$dataset$Sepal.Length)
and a "script" like, to render the site and then all pages except index.
markdown::render_site()
# import data
species = c("versicolor", "setosa")
# filter data and render page
for (i in 1:nrow(species)){
data_filtered <- dplyr::filter(iris, species == species[i])
rmarkdown::render("templates/page.rmd.Rmd", params = list(
data_set = data_filtered,
output_file = paste0("../_site/", pages$species[i], ".html"))
}
Right now, I don't get the header on top of my html as I am not using the common structure....
I could also use another option (R based) if you have ideas. shiny isn't the best option.
Thank you!

Adding navigation to a section in an R markdown file

I have a navbar defined in the _site.yml file:
name: "Rmarkdown with navbar"
output_dir: "."
navbar:
title: "Data Analysis"
type: inverse
right:
- text: "Abstract"
href: https://www.stackoverflow.com
- text: "Data Preparing"
href: https://www.stackoverflow.com
- text: "Related Plots"
href: https://www.stackoverflow.com
- text: "Player Selection"
href: https://www.stackoverflow.com
- text: "Conclusion"
href: https://www.stackoverflow.com
output:
html_document:
theme: spacelab
highlight: textmate
I would like to be able to navigate to each section in my main rmd file, each section is defined as:
<section id="name-of-section">
I tried the following but it doesn't work (in the _site.yml file).
right:
- text: "Abstract"
href: #abstract
I would really appreciate any help. It would be even better if I could add navigation animation when clicking (I know it's achievable using jQuery in websites).
Have you try just adding titles and subtitles to your markdown with '#' and "##' and then search for any section in "online document" (Crtl + Shift + "O")
Or you want a navigation panel on the markdown's output document?
If this is the case, have you try something like this at the beginning of your markdown?
---
title: "El Code-Book de Guibi"
output:
html_document:
toc: yes
toc_depth: 5
toc_float: yes
pdf_document:
toc: yes
toc_depth: '5'
editor_options:
markdown:
wrap: 72
---
In my case, it shows output like this:

Use a specific slide number for slideNumberFormat in xaringan

In a xaringan presentation, the slideNumberFormat argument is often set to %current% / %total%. It is possible to use a specific slide number in this argument?
For instance, if I have a "final" slide like this:
---
name: mylastslide
# Thanks
with appendix slides behind, I want to display slide numbers like %current% / %mylastslide% with %mylastslide% the number of my slide called mylastslide.
Thanks.
[Edit after #user2554330 suggestion]
For this code, with an incremental slide,
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
institute: "RStudio, PBC"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
lib_dir: libs
---
background-image: url(https://upload.wikimedia.org/wikipedia/commons/b/be/Sharingan_triple.svg)
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```
<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
return 'Slide ' + current + ' of ' + (this.getSlideByName("mylastslide").getSlideIndex() + 1); },
highlightStyle: "github",
highlightLines: true,
countIncrementalSlides: false});
</script>
Image credit: [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Sharingan_triple.svg)
---
class: center, middle
# xaringan
---
hello
--
world
--
thanks
--
really
--
byebye
---
name: mylastslide
# Final slide
Install the **xaringan** package from [Github](https://github.com/yihui/xaringan):
```{r eval=FALSE, tidy=FALSE}
devtools::install_github("yihui/xaringan")
```
---
# Appendix
The last slide (ie appendix) is numbered as Slide 6 of 9 (and not Slide 6 of 5) and 9 is the url index of mylastslide. (I used + 1 in the slideNumberFormat function because indexes start at 0.)
Thanks.
You could certainly set the format to a fixed value for the last slide, e.g. %current% / 34. But you can also set it to a Javascript function. (Edited to add:) The tricky part is that you need to include all the options that would normally appear in the nature argument as well. So you want something like
<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideIndex(); },
highlightStyle: "github",
highlightLines: true,
countIncrementalSlides: false});
</script>
You name a slide by putting the text
name: mylastslide
at the bottom of the slide after --- marks. So here's a complete example, based on the first few slides of the xaringan template:
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
institute: "RStudio, PBC"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
lib_dir: libs
---
background-image: url(https://upload.wikimedia.org/wikipedia/commons/b/be/Sharingan_triple.svg)
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```
<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideIndex(); },
highlightStyle: "github",
highlightLines: true,
countIncrementalSlides: false});
</script>
???
Image credit: [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Sharingan_triple.svg)
---
class: center, middle
# xaringan
### /ʃaː.'riŋ.ɡan/
---
class: inverse, center, middle
# Get Started
---
name: mylastslide
# Hello World
Install the **xaringan** package from [Github](https://github.com/yihui/xaringan):
```{r eval=FALSE, tidy=FALSE}
devtools::install_github("yihui/xaringan")
```
This has 5 slides, numbered "1 of 4" to "5 of 4".
Edited to add: As discussed in the comments, this doesn't handle incremental slides properly: getSlideIndex() counts incremental slides separately. We want to use getSlideNumber(), which stays the same on all of them when we use the option countIncrementalSlides: false. However, the online version of remark-latest.min.js doesn't have getSlideNumber() in it, you need to ask for remark-0.15.0.min.js.
You do this with the following YAML:
xaringan::moon_reader:
lib_dir: libs
chakra: https://remarkjs.com/downloads/remark-0.15.0.min.js
After this the code below worked fine:
<script>
var slideshow = remark.create({slideNumberFormat : function (current, total) {
return 'Slide ' + current + ' of ' + this.getSlideByName("mylastslide").getSlideNumber(); },
highlightStyle: "github",
highlightLines: true,
countIncrementalSlides: false});
</script>

Remove toc when printing rmarkdown site

I've used the super versatile Rmarkdown package to create a site for a set of seminars extracted and modified using R.
The _site.yml file looks like this
name: "Seminars at Biostatistics, UCPH"
exclude: ["*.Rmd*", "*.json", "Makefile"]
navbar:
title: "Seminars # Biostatistics, UCPH"
left:
- text: "Upcoming seminars"
icon: fa-lightbulb-o
href: index.html
- text: "Previous seminars"
icon: fa-calendar
href: previous.html
right:
- icon: fa-question fa-lg
href: http://biostat.ku.dk
output:
html_document:
theme: readable
highlight: textmate
include:
after_body: footer.html
toc: true
toc_float: true
css: style.css
When the page is printed on paper then the table-of-content is overlapping the main text which makes it rather useless.
Is it possible to remove the toc when printing? Either directly through arguments in the yaml or possibly through css and #media commands
Of course. In your stylesheet:
#media print {
#TOC { display: none; }
}

Resources