Compose a web-link for markdown pdf

I am writing an R markdown document (from Shiny) where the user can decide on whether a pdf or html or word file shall be created. I am trying to include a composed link, like this:

```{r}
gene <- "ENSG00000139618"
urlEnsembleLink <- a("Ensemble", href=paste0("https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",gene), target="_blank")
 '''
Ensemble URL link to gene: `r urlEnsembleLink`.
[Ensemble URL link to gene](https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=ENSG00000139618){target="_blank"}.

The first part is within a code chunk, the second is in "pure" markdown document. Both versions work in a html document. In pdf, only the 2nd works, the first link is not displayed in the document. However, I need to make a composed link for pdf, where the last past of the link (here: "ENSG00000139618") is variable and set by e.g. "gene" variable in the chunk before. Any suggestions how to compose a link for pdf?

Your urlEnsembleLink is only valid for the html case. Try this:

---
title: "Untitled"
author: "My name"
date: "11/19/2021"
output:
  pdf_document: default
  html_document: default
  html_notebook: default
urlcolor: blue
---

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

```{r}
gene <- "ENSG00000139618"
urlEnsembleLink <-""
if (knitr::is_html_output()) {
  urlEnsembleLink <- a(
    "Ensemble",
    href = paste0(
      "https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",
      gene
    ),
    target = "_blank"
  )
} else {
  urlEnsembleLink <- paste0("\\href{https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",
  gene,"}{","Ensemble}")
}
```

### Chapter 1

Ensemble URL link to gene: 
`r urlEnsembleLink`.  

Ensemble URL link to gene: 
[Ensemble](https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=ENSG00000139618){target="_blank"}.

### References
[How to make clickable links in LaTeX](https://latex-tutorial.com/tutorials/hyperlinks/)  

[R markdown link is not formatted blue when knitted to pdf](https://stackoverflow.com/questions/40892802/r-markdown-link-is-not-formatted-blue-when-knitted-to-pdf)

As said above, if you target multi format then you need to

  • either use Markdown directly so that it is multi format (because Pandoc will convert it to the correct format)
  • Or use directly the syntax of the intended format and use R code to include it conditionally.

You'll find more about Multi format on the Coobookd

Thanks a lot, this helped!
Just for completeness, this is the code for creating a link in a word file:

---
title: "Untitled"
author: "My name"
date: "11/19/2021"
output:
  pdf_document: default
  html_document: default
  html_notebook: default
  word_document: default
urlcolor: blue
---

 ```{r}
#helper function
is_word_output <- function(fmt = knitr:::pandoc_to()) {
  length(fmt) == 1 && fmt == "docx"
}

#request
gene <- "ENSG00000139618"
if (knitr::is_html_output()) {
  urlEnsembleLink <- a(
    "Ensemble",
    href = paste0(
      "https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",
      gene
    ),
    target = "_blank"
  )
} else if (is_word_output()) {
  urlEnsembleLink <- paste0("[Ensemble](https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",gene,")"
  )
} else { #pdf
  urlEnsembleLink <- paste0("\\href{https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",
  gene,"}{","Ensemble}")
}
 '''

Ensemble URL link to gene: 
`r urlEnsembleLink`.  
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.