在Rmarkdown表中包含链接(pdf) [英] Including links within Rmarkdown tables (pdf)

查看:357
本文介绍了在Rmarkdown表中包含链接(pdf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建pdf时,我试图在Rmarkdown的有线"表中包含指向特定网页的链接.

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

该表有4列,我希望链接在第二列中,该列当前包含字符串.该表的输出如下所示;

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

推荐答案

使用paste0,您可以在数据框中构造markdown格式的URL,然后将其传递给kable,如下所示:

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

然后您可以在mpg列中看到结果(蓝色文本),并且如果将鼠标悬停在上方,我会看到URL:

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

如果您想在表格中打印URL并使其可单击,那么您将执行mtcars$mpg <- paste0("[", urls, "](", urls, ")")这样的操作:

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:

这就是你的追求吗? paste0的这种用法非常方便于对表执行各种操作,例如,应用条件格式(如粗体表示有效值)

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)

这篇关于在Rmarkdown表中包含链接(pdf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆