Q:

Follow multiple links with rverst::follow_link()

The below code goes to R Journal's Accepted articles page and downloads the first article that has the word package in it.
library(rvest)
library(magrittr)
url_stem <- html_session("https://journal.r-
project.org/archive/accepted/")
url_paper <- follow_link(url_stem, "package") %>%
  follow_link("package") -> url_article
download.file(url_article$url, destfile = "article.pdf")

What I would like to is to download all the articles that have one or more mathing words from a given set of words.
Since follow_link() takes one expression, I tried to loop through the search terms - taking into account the fact the function returns error in the event it doesn't find a matching link.
library(rvest)
library(magrittr)
url_stem <- html_session("https://journal.r-project.org/archive/accepted/")
search_terms <- c("package", "model", "linear")
tryCatch(
  for(i in search_terms) {
  url_paper <- follow_link(url_stem, search_terms[i]) %>%
  follow_link(search_terms[i]) -> url_article
  download.file(url_article$url, destfile = "article.pdf") # Don't how I would write it as article[i=1,2, ...].pdf
}
)

I am getting the below error:
Error in if (!any(match)) { : missing value where TRUE/FALSE needed

This thread wasn't useful since it solves the case for tags. The problem seems simple and might be solved in easier way, but this is probably because R Journal site is pretty neat. Some sites are rather messy.

A:

If this is the problem you're trying to solve (finding the r journal entries with 'package') and not a smaller example for a larger scraping task for another site, then this is what you can do:
library(xml2)
library(stringi)
library(tidyverse)

doc <- xml_ns_strip(read_xml("https://journal.r-project.org/rss.atom"))

xml_find_all(doc, "//entry[contains(., 'ackage')]") %>% 
  map_chr(~{
    xml_find_first(.x, ".//link") %>% 
      xml_attr("href") %>% 
      stri_replace_last_fixed("/index.html", "") %>% 
      stri_replace_last_regex("/(RJ-.*)$", "/$1/$1.pdf")

##  [1] "https://journal.r-project.org/archive/2017/RJ-2017-003/RJ-2017-003.pdf"
##  [2] "https://journal.r-project.org/archive/2017/RJ-2017-005/RJ-2017-005.pdf"
##  [3] "https://journal.r-project.org/archive/2017/RJ-2017-006/RJ-2017-006.pdf"
##  [4] "https://journal.r-project.org/archive/2017/RJ-2017-008/RJ-2017-008.pdf"
##  [5] "https://journal.r-project.org/archive/2017/RJ-2017-010/RJ-2017-010.pdf"
##  [6] "https://journal.r-project.org/archive/2017/RJ-2017-011/RJ-2017-011.pdf"
##  [7] "https://journal.r-project.org/archive/2017/RJ-2017-015/RJ-2017-015.pdf"
##  [8] "https://journal.r-project.org/archive/2017/RJ-2017-012/RJ-2017-012.pdf"
##  [9] "https://journal.r-project.org/archive/2017/RJ-2017-016/RJ-2017-016.pdf"
## [10] "https://journal.r-project.org/archive/2017/RJ-2017-014/RJ-2017-014.pdf"
## [11] "https://journal.r-project.org/archive/2017/RJ-2017-018/RJ-2017-018.pdf"
## [12] "https://journal.r-project.org/archive/2017/RJ-2017-019/RJ-2017-019.pdf"
## [13] "https://journal.r-project.org/archive/2017/RJ-2017-021/RJ-2017-021.pdf"
## [14] "https://journal.r-project.org/archive/2017/RJ-2017-022/RJ-2017-022.pdf"
## [15] "https://journal.r-project.org/archive/2016/RJ-2016-031/RJ-2016-031.pdf"
## [16] "https://journal.r-project.org/archive/2016/RJ-2016-032/RJ-2016-032.pdf"
## [17] "https://journal.r-project.org/archive/2016/RJ-2016-033/RJ-2016-033.pdf"
## [18] "https://journal.r-project.org/archive/2016/RJ-2016-034/RJ-2016-034.pdf"
## [19] "https://journal.r-project.org/archive/2016/RJ-2016-036/RJ-2016-036.pdf"
## [20] "https://journal.r-project.org/archive/2016/RJ-2016-041/RJ-2016-041.pdf"
## [21] "https://journal.r-project.org/archive/2016/RJ-2016-043/RJ-2016-043.pdf"
## [22] "https://journal.r-project.org/archive/2016/RJ-2016-045/RJ-2016-045.pdf"
## [23] "https://journal.r-project.org/archive/2016/RJ-2016-046/RJ-2016-046.pdf"
## [24] "https://journal.r-project.org/archive/2016/RJ-2016-047/RJ-2016-047.pdf"
## [25] "https://journal.r-project.org/archive/2016/RJ-2016-048/RJ-2016-048.pdf"
## [26] "https://journal.r-project.org/archive/2016/RJ-2016-050/RJ-2016-050.pdf"
## [27] "https://journal.r-project.org/archive/2016/RJ-2016-052/RJ-2016-052.pdf"
## [28] "https://journal.r-project.org/archive/2016/RJ-2016-054/RJ-2016-054.pdf"
## [29] "https://journal.r-project.org/archive/2016/RJ-2016-055/RJ-2016-055.pdf"
## [30] "https://journal.r-project.org/archive/2016/RJ-2016-056/RJ-2016-056.pdf"
## [31] "https://journal.r-project.org/archive/2016/RJ-2016-057/RJ-2016-057.pdf"
## [32] "https://journal.r-project.org/archive/2016/RJ-2016-058/RJ-2016-058.pdf"
## [33] "https://journal.r-project.org/archive/2016/RJ-2016-059/RJ-2016-059.pdf"
## [34] "https://journal.r-project.org/archive/2016/RJ-2016-060/RJ-2016-060.pdf"
## [35] "https://journal.r-project.org/archive/2016/RJ-2016-062/RJ-2016-062.pdf"

The RSS feed is a much more amenable scraping source.
Even if this is not the specific task, I think this line:
xml_find_all(doc, "//entry[contains(., 'ackage')]")

is ultimately what you're after. That finds all entry tags that have that string anywhere in the descendants. You can use XPath boolean logic in the [] (i.e. logically chain multiple contains()).

