R Shiny create a column in reactable table that includes a hyperlink

Reposting this with a better reprex. I have a dataset in which one column is a player name and a separate column is a URL. Is it possible to get the URL embedded in the Player's name in a Shiny Reactable table so someone can click on the Player name and be sent to that URL?

Below is what I currently have as my reprex for the app:

library(shiny)
library(tidyverse)
library(dplyr)
library(reactable)


Player <- c("Draymond Green", "Steph Curry", "Alex Caruso")
Line <- c(10.5, 30.5, 11.5)
URL <- c("https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/bet_rivers_il",
         "https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/bet_rivers_in",
         "https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/draftkings")

sportsbook_odds <- data.frame(Player, Line, URL)


ui <- fluidPage(
    
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "state",
                        label = "Select a state",
                        choices = c(
                            "CO", "IL", "IN", "IA", "MI", "NV", "NH",
                            "NJ", "OR", "PA", "TN", "VA", "WV"),
                        selected = "NJ"),
            selectInput(inputId = "team",
                        label = "Team(s) to display",
                        multiple = TRUE,
                        choices = c("Select team(s)" = "", "ATL", "BKN", "BOS", "CHA", "CHI", "CLE", "DAL", "DEN",
                                    "DET", "GSW", "HOU", "IND", "LAC", "LAL", "MEM",
                                    "MIA", "MIL", "MIN", "NOP", "NYK", "OKC", "ORL", "PHI",
                                    "PHX", "POR", "SAC", "SAS", "TOR", "UTA", "WAS"))),
        
        mainPanel(reactableOutput("bet_values")    
        )
    )
)

server <- function(input, output) {
    
    output$bet_values <- renderReactable({
        reactable(sportsbook_odds, columns = list(
            Line = colDef(align = "center",
                          minWidth = 70)
        ),
        defaultPageSize = 15)
    })
}

shinyApp(ui = ui, server = server)

I want it to just show the Player and Line columns while being able to click on the player's name and be brought to the URL in the URL column. Any help would be appreciated!

It's super easy to include hyperlinks, most text in shiny is HTML and so you just use the <a> tags

https://www.w3schools.com/html/html_links.asp

<a href="url">link text</a>

e.g. 

Player <- paste0('<a href="', URL, '">', Player, '</a>')

So in this scenario, could I just mutate the Player column to be paste0('', data$Player, '')?

Im not sure reactable likes that... i think it will render that as plain text.

server <- function(input, output) {
    
    output$bet_values <- renderReactable({
        reactable(sportsbook_odds[,1:2], columns = list(
            Line = colDef(align = "center",
                          minWidth = 70)
            , Player = colDef(cell = function(Player) {
                url <- sportsbook_odds[Player == Player, "URL"]
                htmltools::tags$a(href = as.character(url), target = "_blank", as.character(Player))
            })
            
        ),
        defaultPageSize = 15)
    })
}

So this is close to having worked! It looks like it concatenates all of the hyperlinks for each player. For example, for Steph Curry the link is c("https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/bet_rivers_il",
"https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/bet_rivers_in",
"https://go.metabet.io/bet/nba_game_player_3_pointers_made/475809/draftkings") instead of just the one link. Is there something I need to do with sapply to get it to work?

Using the reprex provided, my code doesn't do that.

So I suspect it's that your df is a bit more complex. The two places I felt that would be vulnerable are:

sportsbook_odds[,1:2].

That is effectively selecting column 1 & 2 of the data to show just those (i.e. not show URL)..you did spot the comma before the 1:2?

And

Player == Player, "URL"

This is selecting the URL for the Player that is the current Player in the row. But if your players aren't unique... That will fail.

You did spot the change in case from URL to url as well?

I'm assuming your reply was to me :wink:

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.