How to make a link in R Markdown output to get a pdf or HTML with hyperlinks in text

Diego López

I have a data frame output in R Markdown. My problem is that the links column is very long and I would like to make hyperlinks in the column of text ("model" in this case) with the "link" column. In R Markdown it is possible to make a link in plain text but I don't know how to make it in a data output. I want to get a pdf or HTML file.

library (dplyr)
library (data.table)

data <- select(mtcars[1:4,], cyl, gear)
data <- setDT(data, keep.rownames = TRUE)
colnames(data) <-c("model","cyl","gear")
data$link <- paste('http://example.com/',data$model, sep = "")

data
            model cyl gear                              link
1:      Mazda RX4   6    4      http://example.com/Mazda RX4
2:  Mazda RX4 Wag   6    4  http://example.com/Mazda RX4 Wag
3:     Datsun 710   4    4     http://example.com/Datsun 710
4: Hornet 4 Drive   6    3 http://example.com/Hornet 4 Drive

I would like to get the model column with hyperlinks:

model                                      cyl    gear                             
[Mazda RX4](http://example.com/Mazda)        6       4      
[Mazda RX4 Wag](http://example.com/Mazda)    6       4  
[Datsun 710](http://example.com/Datsun)      4       4     
[Hornet 4 Drive](http://example.com/Hornet)  6       3 

But when I do this in R- Markdown I have not results. Is there any way to do it? Thanks in advance.

Ryan Morton

You'll have to work out your format, but you can have R Markdown print results='asis' in the chunk options:

---
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library (dplyr)
library (data.table)

data <- select(mtcars[1:4,], cyl, gear)
data <- setDT(data, keep.rownames = TRUE)
colnames(data) <-c("model","cyl","gear")
data$link <- paste('http://example.com/',data$model, sep = "")
data$markdown_output <- paste0("[", data$model, "](",data$link, ")")
```

## R Markdown Output

```{r output, results='asis'}
data$markdown_output
```

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use eqnarray in R markdown for both html and pdf output?

How to justify (to both sides) text in R Markdown when knitting in pdf output

How do I dynamically adjust for text based on values of multipole columns and set as parameters in R markdown output pdf

R Markdown PDF and fancyhdr - how to add special characters and link to footer

How to rotate a single page to landscape in R Markdown pdf output

How to create a custom example environment for r markdown pdf output?

How to fit the output of Python code in pdf while using R markdown?

Make bold text in HTML output R shiny

How to make a table with inner lines in r markdown when kniting to PDF?

How make 2 column layout in R markdown when rendering pdf?

How to print or cat text into pdf() in R without markdown

How can I get a HTML output instead of markdown?

How to make individual cells of a table into hyperlinks in html?

R Markdown: How do I make text float around figures?

How to show significance stars in R Markdown (rmarkdown) html output notes?

How to create animation in html output using R Markdown

R markdown format title - pdf output

R markdown to PDF - Printing console output

R Markdown - Description next to plot in PDF output

How to display HTML text(as string) on UILabel with hyperlinks?

How to get PDF output to look exactly like HTML output

Knitting/Rendering R Markdown Output: Can I prevent code from showing in PDF output but allow code folding in HTML output?

How to get highlighted text with legends as html output?

In R Markdown, create header/footer on every page regardless of output type (pdf, html, docx)

Want to make hyperlinks to html

how to get figure floated / surrounded by text etc. in R markdown?

How to make a short link to specific code line from markdown in github issue text?

Wrap long text output in R markdown

R markdown adds # link behind HTML headers