Sam
Sam

Reputation: 745

Putting hyperlinks into an HTML table in R

I am a biologist trying to do computer science for research, so I may be a bit naïve. But I would like to a make a table containing information from a data frame, with a hyperlink in one of the columns. I imagine this needs to be an html document (?). I found this post this post describing how to put a hyperlink into a data frame and write it as an HTML file using googleVis. I would like to use this approach (it is the only one I know and seems to work well) except I would like to replace the actual URL with a description. The real motivation being that I would like to include many of these hyperlinks, and the links have long addresses which is difficult to read.

To be verbose, I essentially want to do what I did here where we read 'here' but 'here' points to http:// stackoverflow.com/questions/8030208/exporting-table-in-r-to-html-with-hyperlinks

Upvotes: 1

Views: 2947

Answers (2)

Scott Kaiser
Scott Kaiser

Reputation: 317

Building on Jack's answer but consolidating from different threads:

library(googleVis)
library(R2HTML)
url <- c('http://nytimes.com', 'http://cnn.com', 'http://www.weather.gov')
urlTitles <- c('NY Times', 'CNN', 'Weather')
foo <- data.frame(a=c(1,2,3), b=c(4,5,6), url=url)
foo <- transform(foo, url = paste('<a href = ', shQuote(url), '>', urlTitles, '</a>')) 
x   <- gvisTable(foo, options = list(allowHTML = TRUE))
plot(x)

Upvotes: 1

Jack Harkness
Jack Harkness

Reputation: 810

From your previous question, you can have another list which contains the titles of the URL's:

url=c('http://nytimes.com', 'http://cnn.com', 'http://www.weather.gov'))
urlTitles=c('NY Times', 'CNN', 'Weather'))

foo <- transform(foo, url = paste('<a href = ', shQuote(url), '>', urlTitles, '</a>')) 
x = gvisTable(foo, options = list(allowHTML = TRUE))
plot(x)

Upvotes: 1

Related Questions