如何在 shinnyapp 中将相同的超链接添加到 table 中的值?

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

我的数据有以下 UI 和服务器。我想将数字栏中的值作为超链接,然后点击可以浏览网站。

我想知道是否有任何方法可以在数字列中的值之前添加相同的超链接“https://omim.org/entry/”(它只是一个大的一部分table,所以我不能对所有这些都使用 a(href=))。

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)

如果您只是想为 table 的每一行创建一个带有 link 的列,这可以通过几种方式轻松完成 - 例如,使用 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>"))

然后您只需要记住将 escape=FALSE 添加到您的 datatable() 函数以正确呈现它。

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