glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
627 stars 80 forks source link

Open Hyperlink in New Tab/Window #233

Closed dysonsphere-startmail closed 2 years ago

dysonsphere-startmail commented 2 years ago

I have the following:

"Link"                 = colDef(html = TRUE,
                                  cell = JS("function(cell) {
                                  return '<a href=\' + cell.value + '>' + `Full Text` + '</a>'
                                  }"),
                                  minWidth = 300)

as part of a columns = list in a reactable.

As is, it displays the "Full Text" and the link works to send the browser to the url address in the cell.

However, I am struggling with getting a the link to open in a new tab/window.

I have tried adding target="_blank" rel="noopener noreferrer" but I can't figure out how they fit in the JS.

Can this be done?

Thanks

werkstattcodes commented 2 years ago

this works for me

          column_name=colDef(
            cell=function(value, index) { 
                  url <- sprintf(value)
            htmltools::tags$a(href=url,
                              target="_blank",
                              as.character("link"))}
            )
dysonsphere-startmail commented 2 years ago

this works for me

          column_name=colDef(
            cell=function(value, index) { 
                  url <- sprintf(value)
            htmltools::tags$a(href=url,
                              target="_blank",
                              as.character("link"))}
            )

Using this:

 "Link"                 = colDef(colDef(
                                   cell = function(value, index) {
                                     url <- sprintf(value)
                                     htmltools::tags$a(href=url,
                                                       target="_blank",
                                                       as.character("link"))})
                                 )

I get the following error:

Error in colDef(colDef(cell = function(value, index) { : 
  `name` must be a character string

Checking my data frame, the "Link" column is character.

markschat commented 2 years ago

First - before posting issues on other persons projects:

The answer from @werkstattcodes is working just fine when I remove the nested colDef from the last post:

library(shiny)
library(reactable)

ui <- fluidPage(
  reactableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderReactable(
    reactable(
      data = data.frame(Link = "Google"),
      columns = list(
        Link = colDef(
          cell = function(value, index) {
            url <- sprintf(value)
            htmltools::tags$a(
              href = url,
              target="_blank",
              as.character("link")
            )
          }
        )
      )
    )
  )
}

shinyApp(ui, server)

@dysonsphere If this solution is helpful to you please close the Issue :)

dysonsphere-startmail commented 2 years ago

sorry about missing that obvious copy/paste error and not providing a working example. You suggestion worked. Thanks!