Q:

Parsing charts from a series of webpages

Maybe there is something regarding this subject but I haven t found it, yet.
I have parsed a list of webpages and I am extracting the lists I need from the https://tradingeconomics.com/ website. 
In any page appears a list of countries with an hyperlink for any country to have info on that particular nation. 
Now, in any of these pages there is a chart I would like to extract and store as .pdf or .doc (or any other format available). 
Here's my code.
trading_ec <- read_html("https://tradingeconomics.com/indicators")

## 01. Formatting the link
tr_ec_tabs_39 <- tr_ec_tabs[39]
tr_ec_tabs_lo_39 <- tolower(tr_ec_tabs_39)
tr_ec_nospace_39 <- gsub(" ", "-", tr_ec_tabs_lo_39)

## 02. Choosing dataset (in the ex. : dataset 39, ' food inflation')
json.indicators_39 <- json.indicators[39]

## 03. Scraping the variables names
table_39 <- list()
for(i in seq_along(json.indicators_39))
{
table_39[[1]] <- html_table(read_html(json.indicators_39[1]))[[1]]
}

## 04. Turning the list into a dataframe 
table_39_df <- as.data.frame(table_39)

## 05. Building up the links
table_39_df$Country <- tolower(table_39_df$Country)
table_39_df$Country <- gsub(" ", "-", table_39_df$Country)
json.indicators_39_p <- paste0("https://tradingeconomics.com/" , table_39_df$Country, "/food-inflation")

## 06. Function to choose the tables one is interested in each page
table_39_tables <- list()
for(i in seq_along(json.indicators_39_p))
{
table_39_tables[[i]] <- html_table(read_html(json.indicators_39_p[i]), fill=TRUE)[[1]]
cat("Page", i, "of", length(json.indicators_39_p), "obtained.\n")
}

Any hint?

A:

You can grab each chart quite easily as a PNG image. I will show you the steps you might use.
Firstly load in the necessary libraries, then get a vector containing all the indicators we might want to see by using xpath and html_attr to scrape the links from the page:
library(rvest)
library(tidyverse)

site       <- "https://tradingeconomics.com"
trading_ec <- read_html(paste0(site, "/indicators"))
link_xpath <- "//ul/li/a[contains(@id, 'ctl00_ContentPlaceHolder1')]"
link_nodes <- html_nodes(trading_ec, xpath = link_xpath)
link_names <- grep("country-list", html_attr(link_nodes, "href"), value = T)
indicators <- unlist(lapply(strsplit(link_names, "/"), function(x) x[3]))

These can now be viewed and stored, etc.
print(head(indicators))
# [1] "employed-persons"       "employment-change"      "employment-rate"       
# [4] "full-time-employment"   "initial-jobless-claims" "job-vacancies"

Next we can define a function that will give us a list of all countries available to examine for any given indicator
list_countries <- function(indicator)
{
  url <- paste0("https://tradingeconomics.com/country-list/", indicator)
  indicator_page <- read_html(url)
  country_nodes <- html_nodes(indicator_page, xpath = "//td/a")
  relative_links <- html_attr(country_nodes, "href")
  countries <- unlist(lapply(strsplit(relative_links, "/"), function(x) x[2]))
  tibble(country = countries, 
         link = paste0("https://tradingeconomics.com", relative_links))
}

This allows us to see all the available countries for any indicator, and the link to that country's page.
link_df <- list_countries("food-inflation")
print(link_df)
#> # A tibble: 171 x 2
#>    country     link                                                   
#>    <chr>       <chr>                                                  
#>  1 afghanistan https://tradingeconomics.com/afghanistan/food-inflation
#>  2 albania     https://tradingeconomics.com/albania/food-inflation    
#>  3 algeria     https://tradingeconomics.com/algeria/food-inflation    
#>  4 argentina   https://tradingeconomics.com/argentina/food-inflation  
#>  5 armenia     https://tradingeconomics.com/armenia/food-inflation    
#>  6 aruba       https://tradingeconomics.com/aruba/food-inflation      
#>  7 australia   https://tradingeconomics.com/australia/food-inflation  
#>  8 austria     https://tradingeconomics.com/austria/food-inflation    
#>  9 azerbaijan  https://tradingeconomics.com/azerbaijan/food-inflation 
#> 10 bahrain     https://tradingeconomics.com/bahrain/food-inflation    
#> # ... with 161 more rows

Finally, we define a function to make it easier to get the chart from our target page. We will save this directly to local disk by passing in the desired file name to the function:
get_chart <- function(url, save_to)
{
  page <- read_html(url)
  image_node <- html_nodes(page, xpath = "//img[@id='ImageChart']")
  image_url <- html_attr(image_node, "src")
  download.file(image_url, destfile = save_to, mode = "wb")
}

Now we can save the image directly to local disk. This can be added to a pdf or html as needed.
get_chart(as.character(link_df[1, 2]), path.expand("~/food_chart.png"))

In my R home directory I now have the following file save as food_chart.png:

Of course, you can string the functions together so that you only have to give the country and indicator, etc.

