【问题标题】:R Markdown - Hyperlink outside Rmd fileR Markdown - Rmd 文件外的超链接
【发布时间】:2018-10-10 17:39:07
【问题描述】:

我想知道我们如何在 Rmd 文件之外为 R markdown 定义超链接。在 Rmd 文件中定义超链接就像输入 [hyperlink lable](actual link) 一样简单;但是,如果在 Rmd 文件上,我正在调用其他生成文件地址的 r 文件(例如函数),有没有办法将此信息传输回 Rmd 文件以在那里生成超链接?

请参阅下面的示例以获得更多说明:

Rmd 文件内部:

myFun( with some input)

myFun 内部:

myFun <- function( with some input)
some calculations...
return("[click here](C:/myFile.docx)")

生成的html页面上的输出是:

[1] "[点击这里] (C:/myFile.docx)"

这不是超链接!

【问题讨论】:

    标签: r hyperlink markdown r-markdown


    【解决方案1】:

    要定义rmd 文件之外的链接,您可以使用parameterized report。这允许您在编译时将值传递到rmarkdown 文档中。为此,首先创建一个包含所需参数的rmarkdown 文档,这些参数在yaml 标头中声明,然后在报告中稍后使用。然后,在单独的 R 脚本中,运行 render 命令(来自 rmarkdown 包)并传递所需的参数值。

    这是一个使用catpaste 生成链接的示例。为了比较,我还添加了第二组参数,这些参数使用@JohnCoene 答案中的方法添加不同的链接。我已将rmarkdown 文档保存为"test.rmd",这是在render 命令中识别该文档的方式。

    rmarkdown文档

    ---
    output: html_document
    params:
      text1: "add text"
      link1: "add URL"
      text2: "add text"
      link2: "add URL" 
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ```{r}
    # Function to add link 
    myFun <- function(text, link, inline=TRUE) {
      if(inline) {
        paste("[", text, "](", link, ")", sep="")
      } else {
        cat("[", text, "](", link, ")", sep="")
      }
    }
    ```
    
    Blah, blah, blah, more text. And here's the link: 
    
    ```{r, results="asis"}
    myFun(params$text1, params$link1, inline=FALSE)
    ```
    
    Blah, blah, blah, more text. And here's a way to `r myFun(params$text1, params$link1)`.
    
    Or, using the examples from `@JohnCoene's` answer:
    
    With an HTML tag:
    
    ```{r, results="asis"}
    tg <- function (link, text){
      paste0("<a href='", link, "'>", text, "</a>")
    }
    
    tg(params$link2, params$text2)
    ```
    
    With `htmltools`:
    
    ```{r}
    # install.packages("htmltools")
    library(htmltools)
    
    tags$a(
      href = params$link2,
      params$text2
    )
    ```
    

    单独的 R 脚本来渲染 rmarkdown 文档

    library(rmarkdown)
    
    render(input="test.rmd", 
           params=list(text1="link to Stackoverflow", 
                       link1="https://stackoverflow.com/questions/52745926/r-markdown-hyperlink-outside-rmd-file",
                       text2="link to google",
                       link2="https://google.com"))
    

    输出的 html 文档如下所示:

    【讨论】:

      【解决方案2】:

      有多种解决方案,返回一个html标签或者使用htmltools包。

      HTML 标记

      ```{r, results="asis"}
      tg <- function (link, text){
        paste0("<a href='", link"'>", text, "</a>")
      }
      
      tg("http://google.com", "link to Google")
      ```
      

      html工具

      绝对推荐这种方式优于另一种方式。

      ```{r}
      # install.packages("htmltools")
      library(htmltools)
      
      tags$a(
        href = "https://google.com",
        "Link to Google"
      )
      ```
      

      【讨论】:

        【解决方案3】:

        如问题中所述,假设“myFun”函数的输出是超链接字符串,这对我来说最有效:

        myFun 内部:

        myFun <- function()
        ...some calculations to generate a csv file...
        return("C:/myFile.csv")
        

        Rmd 文件内部:

        ```{r, echo=FALSE}
        myHyperlink <- myFun()
        hyperlink_exists = !is.null(myHyperlink)
        ```
        
        ```{r, eval=hyperlink_exists, results="asis", echo=FALSE}
        cat(paste0("The file is saved ","[", "Here", "](", myHyperlink, ")","\n"))
        ```
        

        【讨论】:

          猜你喜欢
          • 2013-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多