htmlrexporttablehtmlexport-to-html

Export html table in R with recognition of hyperlinks


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") 

Solution

  • 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")