如何使用kableExtra创建的表中的单词替换超链接,并将其显示在pdf文档中

时间:2018-01-29 22:47:22

标签: r pdf kableextra

嗨我在.rmd文档中有这个示例数据框,我想在其中显示一个带有kableExtra包的表。问题是我不能用第一列的“linkname”替换第二列的url,仍然保持它们作为pdf文档中的超链接。

---
title: "Clinical Table"
author: EK
date: January 29, 2018
output: 
  pdf_document: 
  keep_tex: yes
---

```{r nice-tab, tidy=FALSE,echo=FALSE,message=FALSE} 
library(dplyr)
library(kableExtra)
library(rmarkdown)
library(knitr)
  df<-data.frame(rep("linkname",10))
df$url<- c("https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
           "https://www.lincoln.com/luxury-cars/continental/",
           "http://shop.honda.com/civics.aspx",
           "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/",
           "https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
           "https://www.lincoln.com/luxury-cars/continental/",
           "http://shop.honda.com/civics.aspx",
           "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/",
           "https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
           "https://www.lincoln.com/luxury-cars/continental/")
names(df)[1]<-"name"

df$name<- paste("<a href=\"",df$url,"\">",df$name,"</a>",sep="")


kable(
  df, caption = 'REPORT TABLE',
  booktabs = TRUE,format = "latex",escape = FALSE
)%>%
kable_styling(full_width = T)

```

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您试图让一个列“名称”链接到列出的网站,但只显示“链接名称”而不是完整网址。

尝试以下方法。

df<-data.frame(name = rep("linkname",10))
url<- c("https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
       "https://www.lincoln.com/luxury-cars/continental/",
       "http://shop.honda.com/civics.aspx",
       "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/",
       "https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
       "https://www.lincoln.com/luxury-cars/continental/",
       "http://shop.honda.com/civics.aspx",
       "https://bringatrailer.com/2011/12/28/striking-1973-maserati-bora-4-9/",
       "https://en.wikipedia.org/wiki/Cadillac_Fleetwood",
       "https://www.lincoln.com/luxury-cars/continental/")

df$name <- paste0("[", df$name, "](", url, ")")

knitr::kable(df)