如何在Sweave文档中的表中包括超链接? [英] How can I include hyperlinks in a table within an Sweave document?

查看:80
本文介绍了如何在Sweave文档中的表中包括超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含要使用Sweave表示为可点击链接的超链接.我了解xtable,但是不确定如何使用它将数据帧的内容视为LaTeX命令.

I have a data frame containing hyperlinks that I would like to present as clickable links using Sweave. I know about xtable, but am not sure how to use it to treat the contents of a data frame as LaTeX commands.

推荐答案

一种策略是使用xtableprint函数中的sanitize.text.function. 设置sanitize.text.function = function(x){x}会使print仅仅回显数据帧的内容,以供LaTeX稍后解释:

One strategy is to use the sanitize.text.function from the print function in xtable. Setting sanitize.text.function = function(x){x} causes print simply to echo the contents of the data frame for later interpretation by 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输出:

...which produces this PDF output:

这篇关于如何在Sweave文档中的表中包括超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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