Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kable kableExtra, Cells with hyperlinks

I am trying to create a table in an rmarkdown document and target HTML output, using the much appreciated and really awesome kable and kableExtra tools.

The table needs to have cells with hyperlinks. While I can just put the URL's in a column of the rendered table, I would really prefer to use another column as the anchor text and have the column containing the URL's be completely invisible.

For example, let's create a kable table with hyperlinks...

dt      <- mtcars[c(15,16,19,31),1:3] %>% mutate(model=row.names(.))
dt$url  <- c("https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
             "https://www.lincoln.com/luxury-cars/continental/",
             "http://shop.honda.com/civics.aspx",
             "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/")

The most primitive way to render this in kable would be this:

kable(dt, format = "html") %>%
    kable_styling(bootstrap_options = c("hover", "condensed"))

That produces an output that looks like this:

table with URL's

This is almost what I want, but I would like to HIDE the "url" column and instead have the "model" column be a hyperlink to the address given in "url" column. Displaying raw URL's might be OK in this example but my actual URL's are really ugly and not fit for human reading.

Instead, I would like to render something like this: Table with proper hyperlinks

I guess there's two problems here. One is how to suppress rendering a column, the other is how to properly create a hyperlink in cells using column data.


It occurred to after writing this question that I can simply compose the raw HTML for the hyperlinked cells prior to rendering with kable. The key thing is to set escape=FALSE when calling kable().

In other words, I can do this...

dt      <- mtcars[c(15,16,19,31),1:3] %>% mutate(model=row.names(.))
dt$url <- c("https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
             "https://www.lincoln.com/luxury-cars/continental/",
             "http://shop.honda.com/civics.aspx",
             "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/")

dt <- dt %>% mutate(model=paste("<a href=\"",url,"\">",model,"</a>",sep="")) %>% 
select (mpg,cyl,disp,model)

kable(dt, format = "html", escape = FALSE) %>%
      kable_styling(bootstrap_options = c("hover", "condensed")) 

THAT WORKS, but I would prefer to not interleave R and HTML together in such an unappetizing character sandwich.

like image 495
Angelo Avatar asked Jan 29 '18 17:01

Angelo


1 Answers

There is a link option in cell_spec that you can use. See https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html#links

library(dplyr)
library(knitr)
library(kableExtra)
dt_url <- c("https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
            "https://www.lincoln.com/luxury-cars/continental/",
            "http://shop.honda.com/civics.aspx",
            "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/")

mtcars[c(15,16,19,31),1:3] %>% 
  mutate(model = cell_spec(row.names(.), "html", link = dt_url)) %>%
  kable("html", escape = FALSE) %>%
  kable_styling(bootstrap_options = c("hover", "condensed")) 
like image 142
Hao Avatar answered Nov 18 '22 03:11

Hao