R/wiki_inthenews.R

Defines functions wiki_inthenews

Documented in wiki_inthenews

#' Generate random news item
#'
#' @return A message with a random 'in the news' item from Wikipedia's main page
#'
#' @examples wiki_inthenews()

wiki_inthenews <- function() {

  # get url from input and read html
  input <- "https://en.wikipedia.org/wiki/Main_Page"
  wiki_page <- xml2::read_html(input, fill = TRUE)

  # scrape list data
  itn <- wiki_page %>%
    rvest::html_nodes(xpath = '//*[@id="mp-itn"]') %>%
    rvest::html_nodes("li") %>%
    rvest::html_text()

  itn <- itn[nchar(itn) > 40] %>%
    sample(1)

  paste("Here's some news.", itn, "(Courtesy of Wikipedia)") %>%
    message()

}

Try the wikifacts package in your browser

Any scripts or data that you put into this service are public.

wikifacts documentation built on Aug. 1, 2019, 5:06 p.m.