Home > Mobile >  Convert a whole column of urls to clickable urls in a datatable of a shiny app
Convert a whole column of urls to clickable urls in a datatable of a shiny app

Time:12-22

How can I convert a whole column of urls in a dataframe displayed via DT::datatable() into clickable urls that will lead you to the relative webpages?

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)

df<-structure(list(sub_indicator = c("peak concentrations", "Toxics Release Inventory", 
                                     "American Housing Survey"), `Data Sources 1` = c("https://www.epa.gov/outdoor-air-quality-data/air-quality-index-report", 
                                                                                      "https://www.epa.gov/toxics-release-inventory-tri-program/tri-data-and-tools#triadvanced", 
                                                                                      "https://www.census.gov/programs-surveys/ahs/data/interactive/ahstablecreator.html?s_areas=00000&s_year=2021&s_tablename=TABLE1&s_bygroup1=1&s_bygroup2=1&s_filtergroup1=1&s_filtergroup2=1"
                                     )), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
                                     ))


ui <- dashboardPage(
  dashboardHeader(title = "Dataset Inventory"),
  dashboardSidebar(
    
    
    
  ),
  dashboardBody(
    dataTableOutput("table")
  )
)

server <- function(input, output) {
  
  output$table<-renderDataTable({
    datatable(
    df
    )
  })
}

shinyApp(ui, server)

CodePudding user response:

You can make the links clickable by adding in html (<a href = ...></a>) and using almost your identical code except adding escape = false:

library(shiny)
library(shinydashboard)
library(DT)

df <- structure(list(sub_indicator = c("peak concentrations", "Toxics Release Inventory", 
                                     "American Housing Survey"), `Data Sources 1` = c("https://www.epa.gov/outdoor-air-quality-data/air-quality-index-report", 
                                                                                      "https://www.epa.gov/toxics-release-inventory-tri-program/tri-data-and-tools#triadvanced", 
                                                                                      "https://www.census.gov/programs-surveys/ahs/data/interactive/ahstablecreator.html?s_areas=00000&s_year=2021&s_tablename=TABLE1&s_bygroup1=1&s_bygroup2=1&s_filtergroup1=1&s_filtergroup2=1"
                                     )), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
                                     ))

## Function to add HTML language to the links
make_link <- function(x){
  paste0("<a target = '_blank' href= ", x,">",x,"</a>")
}
df$`Data Sources 1` <- make_link(df$`Data Sources 1`)

### Shiny app
ui <- dashboardPage(
  dashboardHeader(title = "Dataset Inventory"),
  dashboardSidebar(
  ),
  dashboardBody(
    dataTableOutput("table")
  )
)

server <- function(input, output) {
  output$table <- renderDataTable({
  df
  }, escape = FALSE) # added in escape = FALSE here
}

shinyApp(ui, server)

enter image description here

CodePudding user response:

We could do

library(purrr)
library(dplyr)
server <- function(input, output) {
  
  output$table<-renderDataTable({
    datatable(
      df %>%
         mutate(`Data Sources 1` = map(`Data Sources 1`, ~ glue::glue('<a href = "{.x}" >click</a>')))
    )
  })
}

-output

enter image description here

  • Related