R Markdown - Hyperlink outside Rmd file

一曲冷凌霜 提交于 2021-02-18 18:56:52

问题


I am wondering how we can define a hyperlink for R markdown outside Rmd file. Defining a hyperlink inside Rmd file is as easy as typing [hyperlink lable](actual link); but, if on the Rmd file, I am calling some other r-file (e.g., a function) that generates an address to a file, is there a way to transfer this information back to Rmd file to generate a hyperlink there?

Please see example below for more clarification:

Inside Rmd file:

myFun( with some input)

Inside myFun:

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

The output on the generated html page is:

[1] "[click here] (C:/myFile.docx)"

Which is not a hyperlink!


回答1:


To define the links outside the rmd file you can use a parameterized report. This allows you to pass values into the rmarkdown document when you compile it. To do this, first create an rmarkdown document that includes the desired parameters, which are declared in the yaml header and then used later in the report. Then, in a separate R script, run the render command (from the rmarkdown package) and pass the desired parameter values.

Here's an example that uses cat or paste to generate the links. For comparison, I've also added a second set of parameters that add different links using the methods in @JohnCoene's answer. I've saved the rmarkdown document as "test.rmd", which is how the document is identified in the render command.

rmarkdown document

---
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
)
```

Separate R script to render the rmarkdown document

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"))

Here's what the output html document looks like:




回答2:


There are multiple solutions, return an html tag or use the htmltools package.

HTML tag

```{r, results="asis"}
tg <- function (link, text){
  paste0("<a href='", link"'>", text, "</a>")
}

tg("http://google.com", "link to Google")
```

htmltools

Definitely recommend this way over the other.

```{r}
# install.packages("htmltools")
library(htmltools)

tags$a(
  href = "https://google.com",
  "Link to Google"
)
```



回答3:


As mentioned in the question, assuming the output of 'myFun' function is the hyperlink string, here is what worked best for me:

Inside myFun:

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

Inside Rmd file:

```{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"))
```


来源:https://stackoverflow.com/questions/52745926/r-markdown-hyperlink-outside-rmd-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!