【问题标题】:How can I include hyperlinks in a table within an Sweave document?如何在 Sweave 文档的表格中包含超链接?
【发布时间】:2014-06-10 03:47:53
【问题描述】:

我有一个包含超链接的数据框,我想使用Sweave 将其显示为可点击链接。我知道xtable,但不确定如何使用它将数据框的内容视为 LaTeX 命令。

【问题讨论】:

    标签: r latex knitr sweave xtable


    【解决方案1】:

    一种策略是使用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 文件中对本地保存的文本文件进行超链接。单击超链接文本时,它应该会弹出该文本文件。你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多