在表格中创建超链接并调整列以包装文本Rmarkdown [英] Create hyperlink in table and adjust column to wrap text Rmarkdown

查看:101
本文介绍了在表格中创建超链接并调整列以包装文本Rmarkdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格内创建超链接,并尝试调整列以换行并生成pdf.

I am trying to create hyperlinks inside a table and also adjust columns to wrap text and generate pdf.

Ex表:

variable<-"testing a long column to wrap testing a long column to wrap "
col1<-"\href{https://www.google.co.uk/}{click here}"
col2<-"[click here](https://www.google.co.uk/)"
col3<-"[click here](https://www.google.co.uk/)"
col4<-"[click here](https://www.google.co.uk/)"
col5<-"[click here](https://www.google.co.uk/)"
test<-data.frame(variable,col1,col2,col3,col4,col5)

当我尝试以下操作时,它会正确提供超链接,但不会调整列宽

When I tried the following it gives the hyperlinks correctly but does not adjust column width

library(kableExtra)
knitr::kable(test,row.names=FALSE) %>%
  column_spec(1, width = "5cm")

但是当我尝试包含format ='latex'时,超链接不起作用,但列宽和换行文本

But when I tried with including format ='latex' then hyperlinks does not work but column width and wrap texts

library(kableExtra)
knitr::kable(test,format ='latex' ,row.names=FALSE) %>%
  column_spec(1, width = "5cm")

请帮助我添加超链接,并在Rmarkdown中调整列宽.

Please help me to add hyperlink and also to adjust column width in Rmarkdown.

推荐答案

kableExtra仅在kable生成原始HTML/PDF代码时有效.这就是为什么当您不指定格式时,column_spec无法正常工作的原因.

kableExtra only works when kable generates raw HTML/PDF codes. That's why when you don't specify format, that column_spec is not working.

指定格式后,此问题变成了如何将链接放入有线(乳胶)"问题.实际上,您已经非常接近答案了.关键是您必须在kable中将escape设置为F,以便它可以保持LaTeX代码不变.另外,您还必须在col1中加倍转义该反斜杠,因为它是"R事物".

After you specified the format, this problem turned into a "How to put links in kable (latex)" question. In fact, you are already very close to the answer. The point is that you have to set escape as F in kable so it can keep the LaTeX code as is. Also you have to double escape that backslash in col1 as it's a "R thing".

variable<-"testing a long column to wrap testing a long column to wrap "
col1<-"\\href{https://www.google.co.uk/}{click here}"
col2<-"\\href{https://www.google.co.uk/}{click here}"
col3<-"\\href{https://www.google.co.uk/}{click here}"
col4<-"\\href{https://www.google.co.uk/}{click here}"
col5<-"\\href{https://www.google.co.uk/}{click here}"
test<-data.frame(variable,col1,col2,col3,col4,col5)

knitr::kable(test,format ='latex',row.names=FALSE, escape = F) %>%
  column_spec(1, width = "5cm")

这篇关于在表格中创建超链接并调整列以包装文本Rmarkdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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