lundi 2 novembre 2020

Rmarkdown output rendering based on condition

I am trying to build in an automated way a Word document that will display a list of Youtube video links I want to watch with Youtube Title video as text and the Youtube url as a hyperlink.

I have used Rmarkdown to do that and it works well with a defined number of urls.
But my issue is that I want that Rmarkdown document to work with any number of urls as input of my Links.txt file.

As an example, the content of the Links.txt file could simply be:

https://www.youtube.com/watch?v=NDDUMon9SJM  
https://www.youtube.com/watch?v=x9iAD3GZyfM  
https://www.youtube.com/watch?v=fHhNWAKw0bY  
https://www.youtube.com/watch?v=jYUZAF3ePFE  
https://www.youtube.com/watch?v=ik4USIChrkY  
https://www.youtube.com/watch?v=HgEGAaYdABA  

Is there a way to use a if condition of some sort to display the exact number of urls each time?

The only post that seems related is this one but I don't really see a working solution.

Here is my current code:

---
title: "Hyperlinks_Word"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library('rvest')
library('magrittr')
library('httr')

```


```{r }

url_proxy <- read.table("Links.txt", header = FALSE, sep = "")

url_proxy <- as.character(url_proxy$V1)

link_test <- list()

for(i in 1:length(url_proxy)){
  
  download.file(url_proxy[i], destfile ='scrape_test.html',quiet = TRUE)
  
  url <- read_html('scrape_test.html')
  
  youtube_title <- url%>%html_nodes(xpath = "//title")%>%html_text()
  youtube_title <- data.frame(matrix(unlist(youtube_title), nrow=length(youtube_title), byrow=T))
  colnames(youtube_title) <- "youtube_title"

  link_test[i] <- as.character(youtube_title$youtube_title)
  
}


```


`r i=1`

[`r link_test[i]`](`r url_proxy[i]`)  

`r i=2`

[`r link_test[i]`](`r url_proxy[i]`)  

`r i=3`

[`r link_test[i]`](`r url_proxy[i]`)  

`r i=4`

[`r link_test[i]`](`r url_proxy[i]`)  

Aucun commentaire:

Enregistrer un commentaire