Set internal document link as table value in Rmarkdown

Viewed 43

I would like to set each value of a column in a datatable as an internal link to a different slide. Below is what I have tried but the link is not selectable.

---
title: "Example"
output: slidy_presentation
  
---

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


```{r message=FALSE, warning=FALSE, include=FALSE}
library(DT)
library(tidyverse)
```

## Table
```{r carstab, echo = FALSE}

datatable(mtcars %>% rownames_to_column(var = "plot") %>% mutate(plot = ("[plot](#/plot)")),escape = TRUE)

```


## plot 

```{r plot, echo = TRUE}
plot(mtcars)
```
1 Answers

mutate(plot = ("[plot](#/plot)")) does not work because the markdown code [plot](#/plot) will not be translated to HTML. Its a character.

You want something like <a href = '#(slide_number)'> This is a link </a> in your DT cells.

You could generate such links using paste0():

datatable(
  mtcars %>% 
    mutate(
      plot = 3,
      plot = paste0("<a href = '#(", plot, ")'>link</a>"),
    ), escape = FALSE
)

The plot column in your DT now contains valid links to slide 3 (modify the first instance of plot in mutate() accordingly to link to different slides).

Related