如何在Sweave文档中的表中包含超链接?

Dav*_*ell 6 latex r sweave xtable knitr

我有一个包含超链接的数据框,我希望将其作为可点击链接使用Sweave.我知道xtable,但不知道如何使用它来处理数据帧的内容作为LaTeX命令.

Dav*_*ell 5

一种策略是利用sanitize.text.functionprint函数xtable.设置 sanitize.text.function = function(x){x}原因print只是回显数据帧的内容,以供LaTeX稍后解释:

\documentclass{article}

\usepackage{hyperref}

\begin{document}
\title{Example of how to include hyperlinks in Sweave with \texttt{xtable}}
\author{David R. Lovell}
\maketitle

<<load-packages, include=FALSE>>=
require(xtable)
@

<<read-data, tidy=FALSE>>=
hits <- read.table(textConnection(
"Count,Link,Title
1031,http://australianbioinformatics.net/jobs,Jobs
796,http://australianbioinformatics.net/,Home"),
stringsAsFactors=FALSE, sep=",", header=TRUE)
@

<<print-xtable, echo = FALSE, results = 'asis'>>=
print(
  xtable(
    hits,
    align="rrll",
    caption="Top content on \\href{http://australianbioinformatics.net}{AustralianBioinformatics.net} in May 2014."
    ), 
  include.rownames=FALSE
  )
@

<<print-xtable-href, echo = FALSE, results = 'asis'>>=
linkedHits <- transform(hits, href=paste("\\href{", Link, "}{", Title, "}", sep=""))
print(
  xtable(
    subset(linkedHits, select=c(Count, href)),
    align="rrl",
    caption="Top content on \\href{http://australianbioinformatics.net}{AustralianBioinformatics.net} in May 2014, 
    now with added hyperlinks."
    ), 
  include.rownames=FALSE,
  sanitize.text.function = function(x){x}
  )

@

\end{document}
Run Code Online (Sandbox Code Playgroud)

...生成此PDF输出: 在此输入图像描述