star
star

Reputation: 775

How to add the same hyperlink to the value in a table in shinnyapp?

I have the below UI and server for my data. I would like to have the value in the number column as a hyperlink, then by clicking can go through the website.

I would like to know if is there any way to add the same hyperlink "https://omim.org/entry/" before the values in the number column (it is just a part of a big table, so I cannot use like a(href=) for all of them).

OMIM<- data.frame(chr=c("chr1","chr1","chr1"),
start=c(10,20,30),end=c(40,54,66),number=c(606857,602421,277180))


> OMIM
   chr start end number
1 chr1    10  40 606857
2 chr1    20  54 602421
3 chr1    30  66 277180

ui <- fluidPage( title = "EnhancerExplorer",
             tags$head(tags$style (
               HTML(' '))),
             shinyjs::useShinyjs(), # needed for download button to work
             tabsetPanel( #type = "pills",
               tabPanel("Phenotype",icon = icon("table"),
                        sidebarLayout (
                          sidebarPanel (
                            p(strong ("Find overlap between query and data"),style = "color:blue;"),
                            br(),
                            fileInput("Phenotype_data_file", "Upload genomic coordinates in .bed/.csv format:", multiple = F, accept = c(".bed",".csv")),
                            actionButton("Phenotype_run", label="Run",icon("paper-plane"), style="color: black; background-color: #06F9E1; border-color: black"),
                            br(),
                            actionButton("Phenotype_add.table", "See results",style="color: black; background-color: white; border-color: black"),
                            br(),br(),
                            actionButton("Phenotype_clear", "Clear All",style="color: gray; background-color: white; border-color: gray"),
                            br(),br(),
                            downloadButton("Phenotype_download_res", "Download results",style="color: white; background-color: gray; border-color: black"),
                            width = "2"),
                          mainPanel(dataTableOutput("Phenotype_overlap.table"),
                                    dataTableOutput("Phenotype_table"))
                        )
               )
               )
             )




server <- function (input, output, session) {
  output$Phenotype_table <- renderDataTable({
    Phenotype_table <-  OMIM %>% unique()
    datatable(Phenotype_table,options = list(orderClasses = TRUE,lengthMenu = c(10,25,50), pageLength = 10, searching = T, rownames = FALSE),rownames = FALSE)
    })
  user_Phenotype.query.data <- reactive({
    req(input$Phenotype_data_file)
    ext <- tools::file_ext(input$Phenotype_data_file$name)
    switch(ext,
       csv = fread(input$Phenotype_data_file$datapath, delim = ",",header=F) %>% 
         dplyr::rename (chr =V1, start=V2, end=V3) %>% setkey(chr, start, end),
       bed = fread(input$Phenotype_data_file$datapath,header=F)%>% 
         dplyr::rename (chr =V1, start=V2, end=V3) %>% setkey(chr, start, end),
       validate("Invalid file; Please upload a .csv or a .bed file")
       )
    })

  Gene_OMIM<-reactive({
    OMIM %>% unique()%>% 
      data.table() %>% setkey(chr, start, end)
    })

  ## Run Analyze
  analyzed_Phenotype <- eventReactive(input$Phenotype_run, {

    req(input$Phenotype_run)
    withProgress(message = 'Analysis in progress', value = 0, {
  
      Phenotype.query_overlap<- foverlaps(user_Phenotype.query.data() ,Gene_OMIM (),  nomatch = 0) %>% 
        unique() %>% dplyr::rename (query.start =i.start, query.end=i.end)
      Phenotype.query_overlap <- Phenotype.query_overlap %>% 
        mutate(Overlap.start = Phenotype.query_overlap[, ifelse(start > query.start, start, query.start)]) %>% 
        mutate(Overlap.end = Phenotype.query_overlap[, ifelse(end < query.end, end, query.end)]) %>%
        mutate(Overlap.length = Overlap.end - Overlap.start)
      })
    })

  v_Phenotype <- reactiveValues(table=NULL)

  observeEvent(input$Phenotype_add.table, {
    v_Phenotype$table <- req(DT::datatable(analyzed_Phenotype(),options = list(orderClasses = TRUE,lengthMenu = c(10,25,50), pageLength = 10)))
    },ignoreInit = TRUE)

  observeEvent(input$Phenotype_clear, {
    v_Phenotype$table <- NULL
    },ignoreInit = TRUE)

  output$Phenotype_overlap.table <- renderDT({ v_Phenotype$table }) 

  shinyjs::disable("Phenotype_download_res")
  observeEvent(analyzed_Phenotype(), {
    shinyjs::enable("Phenotype_download_res")
    })

  output$Gene_download_res <- downloadHandler(
    filename = ".csv",
    content = function(file) {
      write.csv(analyzed_Phenotype(), file, row.names = FALSE)
      }
  )
  }

shinyApp(ui, server)

Upvotes: 0

Views: 89

Answers (1)

cnbrownlie
cnbrownlie

Reputation: 632

If you're asking about just creating a column with the link for each row of the table, this can easily be done in a few ways - for example, using dplyr:

OMIM<- data.frame(chr=c("chr1","chr1","chr1"),
                  start=c(10,20,30),end=c(40,54,66),number=c(606857,602421,277180)) %>%
  mutate(link = paste0("<a href='https://omim.org/entry/", number, "'>Link</a>"))

Then you just need to remember to add escape=FALSE to your datatable() function in order to render it correctly.

datatable(Phenotype_table, ..., escape = FALSE)

Upvotes: 1

Related Questions