简体   繁体   中英

Link to a local html file on RMarkdown with Shiny

I have an interactive RMarkdown document with shiny (ie 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM