5

I have an interactive RMarkdown document with shiny (i.e. with the line runtime: shiny in the YAML header) and inside it I would like to create a link to a local html file. But nothing I have tried so far works.

For the sake of the example let's say I have the following files in my working directory:

  • work_dir/
    • rmarkdown_with_shiny.Rmd
    • shiny_app.R
    • www/
      • my_file.html

What I want to do is to create a link inside rmarkdown_with_shiny.Rmd that opens the file www/my_file.html when clicked.

The code inside the file rmarkdown_with_shiny.Rmd is as follows, includes everything I have tried but so far nothing has worked:

    ---
    title: "Rmarkdown with shiny"
    output: html_document
    runtime: shiny
    ---

    [link_1](www/my_file.html)
    [link_2](my_file.html)
    [link_3](file://www/my_file.html)

    ```{r shiny_links, echo=F, eval=T}
    renderUI(tags$a("link_4", href="my_file.html", target="_blank"))
    renderUI(tags$a("link_5", href="www/my_file.html", target="_blank"))
    renderUI(tags$a("link_6", href="file://www/my_file.html", target="_blank"))

    shinyAppFile("shiny_app.R")
    ```

With the last line shinyAppFile("shiny_app.R") I can embed an app that contains a working link (when the app is ran alone), but as soon as it is embedded it doesn't work anymore. This is the code inside the shiny_app.R:


    library('shiny')

    ui <- fluidPage(
        htmlOutput("link")
    )

    server <- function(input, output) {
      output$link <- renderUI(tags$a("single_link", href="my_file.html", target="_blank"))
    }
    shinyApp(ui = ui, server = server)

The confusing part is that this line [link_1](www/my_file.html) would work if it was only rmarkdown without shiny. And this line would work if it was only a shiny app renderUI(tags$a("single_link", href="my_file.html", target="_blank")). But in a rmarkdown file with runtime: shiny neither of those work.

I would appreciate it a lot if someone could tell me then how to link local html files in rmarkdown + shiny files. Specially if there is a way to do it using shiny functions rather than markdown syntax. But either solution is well welcomed, as long as it creates a functioning link.

  • 2
    I know that you say the file is named `rmarkdown_with_shiny.Rmd`, but just want to check that it's absolutely true. I've run into some weird behavior with `runtime: shiny` when the file name contains spaces. So my piece of advice is to make sure there are no spaces or non-ascii characters in the filename. – JasonAizkalns Feb 20 '20 at 18:55
  • Yes, the name of the file contains only letters and underscores, it is exactly `rmarkdown_with_shiny.Rmd`. No spaces or non-ascii characters. – Anaconda Real Feb 21 '20 at 13:03

1 Answers1

3

Basically, when we run Shiny app the contents of www folder are internally embedded and we don´t need to include www folder to the href attribute. But, if we want to "expose" those contents thru runtime: shiny we need to add shiny::addResourcePath() function and specify its folder:

    ---
    title: "Rmarkdown with shiny"
    output: html_document
    runtime: shiny
    ---

    ```{r setup, include = FALSE}
    library(knitr)
    library(shiny)
    library(here)
    shiny::addResourcePath(prefix = "www", directoryPath = here::here("www"))
    ```

    Relative File Path: [My HTML file](www/my_file.html) 

    Relative File Path: <a href = "www/my_file.html" target="_blank">My HTML file</a>  
    Absolute File Path: <a href = "http://www.shinyapps.io/" target="_blank">shinyapps.io</a>

    Relative File Path: 
    ```{r shiny-relative-links, echo = FALSE, eval = TRUE}
    tags$a(href = "www/my_file.html",
       tags$span(style = "color: #03a9f4", "My HTML file"),
       target = "_blank")
    ```

    Absolute File Path:
    ```{r shiny-absolute-links, echo = FALSE, eval = TRUE}
    tags$a(href = "http://www.shinyapps.io/",
       tags$span(style = "color: #03a9f4", "shinyapps.io"),
       target = "_blank")
    ```

See here for original solution and discussion. Also, Ode to the here package.

Radovan Miletić
  • 2,521
  • 1
  • 7
  • 13