Export html table in R with recognition of hyperlinks

807 Views Asked by At

I know this question has been asked before few times, but the answers provided are not fitting my task unfortunately. this is an example: exporting table in R to HTML with hyperlinks

I need to export dataframe from R to html while providing hyperlinks that I can click to open the website. The 2 formats below only export as text not active hyperlinks.

the expected output is that in the html: the column link has web address as a link I can click one and the column link2 shows the name and when I click on it, it opens the same link. What happens now is that I see an inactive link in column "link" and see the HTML code in the column "link2".

1   wiki    https://en.wikipedia.org/wiki/Main_Page <a href="https://en.wikipedia.org/wiki/Main_Page" target="_blank">wiki</a>
2   google  https://www.google.com/ <a href="https://www.google.com/" target="_blank">google</a>

I tried to dig into the package arguments, but could not find an answer. https://cran.r-project.org/web/packages/tableHTML/tableHTML.pdf

thank you

This is the code I am using.

name = c("wiki", "google")
link = c("https://en.wikipedia.org/wiki/Main_Page", "https://www.google.com/")

df = data.frame(name, link)
df$link2 = paste0("<a href='",df$link,">",df$name,"</a>", sep="")

tableHTML::write_tableHTML(tableHTML(df), file = "df.html") 
3

There are 3 best solutions below

0
On BEST ANSWER

After doing some extra reading, I think I found a way that is not ideal but it works.

This is the code that worked:

library(tableHTML)

name = c("wiki", "google")
link = c("https://en.wikipedia.org/wiki/Main_Page", "https://www.google.com/")

df = data.frame(name, link)
 
df$link2 = paste('<a href="', df$link, '" target="_blank">', df$name, '</a>', sep="")


tableHTML::write_tableHTML(tableHTML(df, escape = F), file = "df.html") 
1
On

I may not be understanding your question correctly, but if you just want your table with hyperlinks to be rendered in html with working links, an easy way to do this is to create a markdown file, and then "knit" it to html in rstudio.

---
title: "table_to_html"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
name = c("wiki", "google")
link = c("https://en.wikipedia.org/wiki/Main_Page", "https://www.google.com/")

df = data.frame(name, link)

df %>% kable() %>% kable_styling()

When you knit this to html, you will get a nice html file with working hyperlinks.

1
On

Thank you very much for your question. It has inspired us to create a default function in the new version 2.1.0 of tableHTML, that we have published yesterday.

You can now use the function make_hyperlink.

# install.packages("tableHTML") # make sure version 2.1.0 is used

library(tableHTML)

name <-  c("wiki", "google")
link <- c("https://en.wikipedia.org/wiki/Main_Page", "https://www.google.com/")

Then use the new function to create the HTML. It takes the hyperlink, i.e. href. You can also pass a second argument to use as a name:

hyperlinks <- make_hyperlink(link, name)

Then proceed creating a tableHTML:

df = data.frame(name = hyperlinks)

df %>% 
 tableHTML(rownames=FALSE,
           escape=FALSE) %>% 
 tableHTML::write_tableHTML(file = "df.html") 

The result looks like this:

<table style="border-collapse:collapse;" class=table_1620 border=1>
<thead>
<tr>
  <th id="tableHTML_header_1">name</th>
</tr>
</thead>
<tbody>
<tr>
  <td id="tableHTML_column_1"><a href=https://en.wikipedia.org/wiki/Main_Page>wiki</a></td>
</tr>
<tr>
  <td id="tableHTML_column_1"><a href=https://www.google.com/>google</a></td>
</tr>
</tbody>