我该如何在Sweave文档的表格中添加超链接?

6

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

1个回答

5

一种策略是使用xtableprint函数的sanitize.text.function。 将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}

这个操作会生成一个PDF输出文件: 输入图像描述


这是一个很好的答案。我想在HTML文件中创建一个指向本地保存的文本文件的超链接。点击超链接文本后,应该弹出该文本文件。你能帮我吗? - Bhavneet sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接