## In the same source file (to remind you that you did it) add:
if (getRversion() >= "2.15.1") utils::globalVariables(c("."))
#' Get news based on sources category, language and country
#' @param category String category
#' @param language String language
#' @param country String country
#' @param apiKey String apiKey
#' @param endpoint String of endpoints
#' @return dataframe
#' @importFrom curl has_internet
#' @importFrom httr http_error GET content status_code
#' @importFrom purrr map
#' @importFrom plyr rbind.fill
#' @importFrom magrittr %>%
#' @importFrom utils globalVariables
#' @examples
#'\dontrun{get_sources(category = 'general',language = 'en')}
#' @export
get_sources <- function(category, language, country, apiKey = newsapi_key(), endpoint = "sources") {
argg <- as.list(environment())
if ((curl::has_internet())) {
if (missing(language) & missing(country) & missing(category)) {
warning("Please pass one of the following sources: category, language, country. Otherwise all sources are displayed.",
call. = FALSE)
}
if (!missing(language)) {
if (!all(language %in% c("ar", "en", "de", "es", "fr", "he", "it", "nl",
"no", "pt", "ru", "sv", "ud"))) {
stop("language must be one of these: ar, en, cn, de, es, fr, he, it, nl, no, pt, ru, sv, ud")
}
}
if (!missing(country)) {
if (!all(country %in% c("ar", "au", "br", "ca", "cn", "de", "es", "fr",
"gb", "hk", "ie", "in", "is", "it", "nl", "no", "pk", "ru", "sa",
"sv", "us", "za"))) {
stop("country must be one of these:, ar, au, br, ca, cn, de, es, fr, gb, hk, ie, in, is, it, nl, no, pk, ru, sa, sv, us, za")
}
}
if (!missing(category)) {
if (!all(category %in% c("business", "entertainment", "gaming", "general",
"health-and-medical", "music", "politics", "science-and-nature",
"sport", "technology"))) {
stop("category must be one of these: business, entertainment, gaming, general, health-and-medical, music, politics, science-and-nature, sport, technology")
}
}
source_url <- create_url(endpoint, argg)
resp <- httr::GET(source_url)
if (httr::http_error(resp)) {
stop("Newsapi request failed ", httr::status_code(resp))
call. = FALSE
} else {
cat("dowloading...\n")
source_list <- jsonlite::fromJSON(httr::content(resp, "text"))
cat("finished dowloading...\n")
}
} else {
stop("No local internet connection available", call. = FALSE)
}
source_df <- source_list %>% purrr::map(~data.frame(.)) %>% do.call(plyr::rbind.fill,
.)
return(source_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.