How to link to a file from an included markdown document in a shiny app?

Hi,

My file (a pdf) is stored in www/.

The app:

library(shiny)
library(here)
shinyApp(
    ui = fluidPage(includeMarkdown(here::here("www/rmarkdown.Rmd"))), 
    server = function(input, output) {}
)

My www/rmarkdown.Rmd contains:

[someFile](./someFile.pdf)
[someFile](someFile.pdf)
[someFile](www/someFile.pdf)
[someFile](file:///someFile.pdf)
[someFile](file:///www/someFile.pdf)

What is the right syntax to link to the pdf file? I tried many variations, none worked.

Any idea what's the proper way to link to (non-image) files from an included Markdown doc?

Hi @Julien,

Are you trying to hyperlink a PDF file inside your R Markdown? What would you want the expected behaviour to be? Opening the PDF file in a separate window?

You may also consider embedding the PDF file inside the HTML file with one of the following commands:

---
title: "Sweet document"
output: html_document
---

![](www/someFile.pdf)

```{r out.width="100%", out.height="500"}
knitr::include_graphics('www/someFile.pdf')
```

A fully-scrollable and downloadable PDF is embedded directly in the HTML document. One using Markdown syntax, the other (my preferred) using knitr and chunk opts.

Otherwise, you are probably better off creating a downloadButton/Handler and allow users to download the PDF locally. It's another option.

Thanks Matt!
Yes, I'm just trying to hyperlink to a pdf, very basic stuff.
But I can't figure out the right syntax in the case of markdown content loaded from an includeMarkdown() call.

Here are a few equivalent ways that work for me:

---
title: "Sweet document"
output: html_document
---

<a href=www/someFile.pdf>text</a>

```{r}
htmltools::HTML('<a href=www/someFile.pdf>text</a>')
```

```{r}
htmltools::a(href = 'www/someFile.pdf', 'text')
```

Thanks Matt. None of it is working though.

I even simplified the app a step further linking directly from the ui function but no luck.

library(shiny)
library(htmltools)

library(shiny)
ui <- fluidPage(
  shiny::a("one",target="_blank", href="www/someFile.pdf"),
  br(),
  htmltools::HTML('<a href=www/someFile.pdf>two</a>'),
  br(),
  htmltools::a(href = 'www/someFile.pdf', 'three'),
)
server <- function(input, output) {}
  
shinyApp(ui, server)

My project folder contains:

dummy_app.Rproj
app.R
www/someFile.pdf

Still get a "Not found" error message in the browser for all links when clicked. :thinking:

OK, I found the solution.
I needed to add shiny::addResourcePath('www', here::here("www")) to my server function. I thought it was taken care of automatically by Shiny.

3 Likes

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