8

Inspired by this answer, I would like to use for example this dataframe in R

input <- data.frame(text = c("a", "b", "c"), 
                    page_number = c(3, 5, 6))

to create a list out of the text, which links to the different page_numbers. The solution describes how to use JavaScript in Markdown but unfortunately not how to use it within code chunks (which is necessary to dynamically create a list).

Patrick Balada
  • 1,330
  • 1
  • 18
  • 37

3 Answers3

6

Sorry I can't comment but do you want the javascript chunks? if so, does this help you?

First

install.package(knitr)

Then in your R Markdown file :

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
```

and

```{js}
some javascript code in here
```
musicformellons
  • 12,283
  • 4
  • 51
  • 86
Gainz
  • 1,721
  • 9
  • 24
  • Thanks for your answer. I tried to use Slide 4 within the js chunk. Unfortunately, it doesn't work. Any suggestion? – Patrick Balada Jan 10 '19 at 13:52
  • Can you try to install the package **knitr** and add the code library(knitr) to your r setup in your R markdown file? Edit : Maybe this can help you : https://rmarkdown.rstudio.com/authoring_knitr_engines.html%23sql – Gainz Jan 10 '19 at 13:57
  • Incase anyone is wondering how to wrap the javascript part up in a file and reference it in the yaml header maybe having a look at [this](https://stackoverflow.com/questions/33543778/call-javascripts-into-rmarkdown-yaml) may be helpful. – Patrick Jun 27 '23 at 14:17
5

The easiest way is to use results='asis' on a code block which creates your desired raw string. You can wrap it into a raw HTML block to make sure that it's not read as Markdown but treated as HTML.

~~~{=html}
```{r, echo=FALSE, results='asis'}
input <- data.frame(text = c("a", "b", "c"), 
                    page_number = c(3, 5, 6))
links <- paste('<a href="', input$text, '">', input$page_number, "</a>", sep="")
cat(links, sep = "\n")
```
~~~
tarleb
  • 19,863
  • 4
  • 51
  • 80
5

I like to use the htmltools package for this kind of job.

You can create a HTML anchor with htmltools::a().
In order to create a link to slide #2, you need to write:

htmltools::a("text", href = "javascript:slidedeck.loadSlide(2)")

You can easily vectorize this expression. Don't forget to pass the resulting list to htmltools::tagList().
Here's a minimal Rmd with the example referenced in the question:

---
title: "Presentation"
output: ioslides_presentation
---

## Slide 1 - page 2

This is the first slide. With links to other slides:

```{r echo=FALSE}
input <- data.frame(text = c("a", "b", "c"), 
                    page_number = c(3, 5, 6))

htmltools::tagList(
  mapply(
    htmltools::a, 
    input$text, 
    href = sprintf("javascript:slidedeck.loadSlide(%i)", input$page_number), 
    SIMPLIFY = FALSE)
)
```
\

If you prefer the tidyverse:

```{r echo=FALSE, message=FALSE}
library(tidyverse)

tribble(
  ~text, ~page_number,
  "a",   3,
  "b",   5,
  "c",   6
) %>%
  transmute(
    text, 
    href = str_glue("javascript:slidedeck.loadSlide({page_number})")
  ) %>%
  pmap(~ htmltools::a(.x, href = .y)) %>%
  htmltools::tagList()
```

## Slide 2 - page 3

Text for slide 2

## Slide 3 - page 4

Text for slide 3

## Slide 4 - page 5

Text for slide 4

## Slide 5 - page 6

Text for slide 4
RLesur
  • 5,810
  • 1
  • 18
  • 53