Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

638
Visualizações
Hyperlink to cell value in R shiny (with many columns)

I want to generate hyperlinks from my datatable values. I've found this solution How to Create hyperlink to cell value in R shiny and was perfect but was for just one column. Now I need also an alternative where hyperlinks are in every columns. Putting targets = "_all" in columnDefs, made hyperlink in each cell, but my problem is that if I click on that cell value (for example on LPE(18:1)), the generated link is composed of every value on that row. I think the solution is to pass the row and column index to the render function, but I have no idea how to do it. Here's a reproducible example.

library(shiny)


data_input <- data.frame(
  List = c("Cer(d18:1/16:0)", "DGDG(16:0/18:0)", "PS(18:0/18:1)"), 
  Feature = c("LPE(18:1)", "LPE(20:4)",NA ),
  Location = c("MGDG(16:0/18:0)", "MGDG(18:0/18:0)" , "TG(18:1/18:1/18:1)")
)

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  "    var a = '<a href=\"http://www.swisslipids.org/#/search/' + row + '\">' + data + '</a>';",
  "    return a;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)



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(data_input, rownames = T, 
              options = list(
                columnDefs = list(
                  list(targets = "_all", render = JS(render)),
                  list(targets = "_all", className = "dt-center")
                )
              )
    )
  })
  
}

shinyApp(ui, server)

Furthermore, by default if a cell value is NA, that cell in the datatable will be blank, but in this case it's written null and has also an hyperlink. Is there a solution to not generate hyperlink for those cells? I just want that they remain blank?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

@Stéphane Laurent will know better than me, but you could try this as your JS renderer. You can check if data is NULL if your if statement, and probably should include data in your hyperlink reference instead of "row". Try this out and and let me know if this helps.

render <- c(
  "function(data, type, row){",
  "  if(type === 'display' && data){",
  "    var a = '<a href=\"http://www.swisslipids.org/#/search/' + data + '\">' + data + '</a>';",
  "    return a;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)

Edit: If you have "Cer(d18:1_16:0)+OH" as an entry in a cell, but want to use "Cer(d18:1/16:0)" in your link, you could use the replace function and chain them for multiple replacements. In this case use /g for global modifier. You can adjust the regex for your particular needs.

render <- c(
  "function(data, type, row){",
  "  if(type === 'display' && data){",
  "    var newstr = data.replace(/_/g, '/').replace(/\\).*/g, ')');",
  "    var a = '<a href=\"http://www.swisslipids.org/#/search/' + newstr + '\">' + data + '</a>';",
  "    return a;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda