4

I have a data table from the DT package that contains multiple columns, one of which contains URLs. Is there a way that I can get these URL's to display as hyperlinks that a user can click on inside a Shiny app? Additionally, could it be so that if the URL is incredibly long (like the random google search that is the 4th entry), only the first 25 characters are displayed without breaking the functionality of the hyperlink?

Some sample code is below.

require(DT)
require(shiny)

web_names <- c("google", "yahoo", "Stack Overflow", "Random Google Search")
url <- c( "https://www.google.com/", "https://www.yahoo.com/", "https://stackoverflow.com/", "https://www.google.com/search?q=random+google+search&oq=random+google+search&aqs=chrome..69i57j0l5.3264j0j7&sourceid=chrome&ie=UTF-8")
websites <- data.frame(web_names, url)

ui <- fluidPage(
  DT::dataTableOutput("websitesTable")
)


server<-function(input,output,session) {
output$websitesTable <- DT::renderDataTable({datatable({websites})})
}

shinyApp(ui=ui, server=server)

UPDATE: Based on the suggested post from Ryan Morton, I have tried adapting the code. My issue is now the sprintf function contained within the user created createLink function. The link button appears, but does not go to the desired location.

#app.R#

library(shiny)

web_names <- c("google", "yahoo", "Stack Overflow", "Random Google Search")
url <- c( "https://www.google.com/", "https://www.yahoo.com/", "https://stackoverflow.com/", "https://www.google.com/search?q=random+google+search&oq=random+google+search&aqs=chrome..69i57j0l5.3264j0j7&sourceid=chrome&ie=UTF-8")
websites <- data.frame(web_names, url)
createLink <- function(val) {
  sprintf('<a href="" target="_blank" class="btn btn-primary">Info</a>', val)
}

websites$url_link <- createLink(websites$url)

ui <- fluidPage(  
  titlePanel("Table with Links!"),
  sidebarLayout(
    sidebarPanel(
      h4("Click the link in the table to go to the url listed.")
    ),
    mainPanel(
      dataTableOutput('table1')
    )
  )
)

server <- function(input, output) {

  output$table1 <- renderDataTable({ datatable({websites})
    return(websites)

  }, escape = FALSE)
}

shinyApp(ui, server)
User247365
  • 665
  • 2
  • 11
  • 27
  • Is this similar: https://stackoverflow.com/questions/28117556/clickable-links-in-shiny-datatable – Ryan Morton Jun 15 '17 at 15:14
  • @RyanMorton That is close and having a button as a link would solve my problem with long urls. It looks like the link is created using the sprintf function which upon initial research from the function documentation, is just a way of formatting the text to be used in a link. I believe it is the href function that creates a link, but that is where I am struggling, adapting that answer to use href that takes existing an existing web address that is currently a character string, and make it a hyperlink. – User247365 Jun 15 '17 at 15:36

1 Answers1

2

Slightly adjust the provided code and it should yield the desired output:

createLink <- function(val) {
  sprintf(paste0('<a href="', URLdecode(val),'" target="_blank">', substr(val, 1, 25) ,'</a>'))
}
websites$url <- createLink(websites$url)

HTML works like this: <a href="LINK", otherOptions,...> Linktext </a> So you can paste your link together with paste0() and substr().

User247365
  • 665
  • 2
  • 11
  • 27
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • This works beautifully! Is there any way that instead of displaying the first 25 characters, a button is created like the post that Ryan Morton posted in the comments? I'm being greedy now as the answer that you have provided works perfectly as I asked in my original post :) – User247365 Jun 15 '17 at 20:23
  • You have now both approaches and i guess you now how `ifelse()` works ;) – Tonio Liebrand Jun 15 '17 at 20:46
  • I needed to use some `if` - `else` logic to deal with the case of missing or zero length URLs. – jsta May 07 '18 at 00:07