Putting hyperlinks into an HTML table in R

流过昼夜 提交于 2019-12-23 05:05:43

问题


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


回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/17155382/putting-hyperlinks-into-an-html-table-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!