2

I want to include a link to a local html file, which lives inside the www directory of my shiny app, inside a column in data.table. On click a new tab should open showing the html file. I've found solutions for linking to internet pages, but how do I adjust this, so that Shiny finds the local files, when rendered in a browser?

This is my code

library(DT)
library(shiny)

link <- "www/my_html.html"
link <- paste0("<a href='", link,"' target='_blank'>", link,"</a>")  # works fine for global url, but not for local file
df <- data.frame(a = 10.5, b = 48, link = link)

ui <- fluidPage(
  DT::dataTableOutput('table1')
)

server <- function(input, output) {
  output$table1 <- DT::renderDataTable({df}, escape = -3)
}

shinyApp(ui, server)
needRhelp
  • 2,948
  • 2
  • 24
  • 48

2 Answers2

2

Maybe you could try running your app using a shiny folder. Make sure your my_html.html file is located in a www folder in your shiny folder.

ui.R

library(DT)
library(shiny)

fluidPage(
  DT::dataTableOutput('table1')
)

server.R

library(DT)
library(shiny)

df <- data.frame(a = 10.5, b = 48, link = "<a href='my_html.html' target='blank' >MyFile</a>")

function(input, output) {
  output$table1 <- DT::renderDataTable({df}, escape = FALSE)
}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Nice, it works with your code. It would also work with escape = 3 (I got this the wrong way round). But the code fails when I have an html file, which is called "my html" with an empty space in between. What do I have to change, so that this works? – needRhelp Jan 22 '17 at 18:13
  • this http://stackoverflow.com/questions/4172579/html-href-syntax-is-it-okay-to-have-space-in-file-name might help you :-) – MLavoie Jan 22 '17 at 18:44
0

I think the main problem with your code is that you are specifying the address of your html file as link <- "www/my_html.html". You should drop out the www/.

It is true that inside your app directory you must have a www directory, and your html files should be inside this www directory. But to properly address your files you should think as if your working directory was already inside the www/ directory.

If you have your shinny app in a single app.R file or a combination of ui.R + server.R doesn't matter, both ways work.

The other detail is in the escape parameter of the renderDataTable() function. It should not be equal to -3, use instead: DT::renderDataTable({df}, escape = FALSE)

So the final code would look like this (supposing you have two html files):

library(shiny)

link <- c("my_html_1.html", "my_html_2.html")
link <- sprintf('<a href="%s" target="_blank">click_here</a>', link)

df <- data.frame(name = c("1st_file", "2nd_file"), 
    value = c(10.5, 48), 
    link = link)

ui <- fluidPage(
  dataTableOutput('table1')
)

server <- function(input, output) {
  output$table1 <- renderDataTable({df}, escape = FALSE)
}

shinyApp(ui, server)