Including links within Rmarkdown tables (pdf)

两盒软妹~` 提交于 2019-12-17 19:48:51

问题


I am trying to include links to particular webpages in a 'kable' table in Rmarkdown, when creating a pdf.

The table has 4 columns, and I wish for the links to be in the second column, which currently includes strings. The output of the table is given below;

    knitr::kable(ind_rank_table_final,row.names = FALSE,caption = "Industry Rank",align = rep("l",ncol(ind_rank_table)))

回答1:


Using paste0, you can construct markdown-formatted URLs in your dataframe, and then pass that to kable, like so:

---
output: pdf_document
---
```{r}
# some urls
urls <- rep("https://stackoverflow.com/", 10)
# use paste0 to compose markdown-formatted hyperlinks
mtcars$mpg <- paste0("[", mtcars$mpg, "](", urls, ")")
# print the table, with hyperlinked text
knitr::kable(head(mtcars))
```

And you can see the result, blue text in the mpg column, and if I hover my mouse over, I see the URL:

If you want to print the URLs in the table, and have them clickable, then you'de do something like this mtcars$mpg <- paste0("[", urls, "](", urls, ")") like so:

Is that what you're after? This use of paste0 is pretty handy for doing all sorts of things to tables, for example, combining multiple values in one cell, and applying conditional formatting (like bold for significant values)



来源:https://stackoverflow.com/questions/35078430/including-links-within-rmarkdown-tables-pdf

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