4

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") 
Bahi8482
  • 489
  • 5
  • 15

3 Answers3

2

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") 
Bahi8482
  • 489
  • 5
  • 15
2

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>
clemens
  • 6,653
  • 2
  • 19
  • 31
  • thank you for the answer and for creating and updating this very useful package. The method you showed is very convenient. – Bahi8482 Mar 23 '21 at 02:57
1

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.

Joe Erinjeri
  • 1,200
  • 1
  • 7
  • 15
  • Thanks for taking the time to review my problem and post the answer. You approach works and I am sure I will benefit from it when I use markdown. For this particular task, I need to use just a regular script without markdown. Sorry if the question was not clear ( I edited the question). – Bahi8482 Mar 15 '21 at 03:14