3
votes

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)
1
@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

1 Answers

1
votes

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().