# getSymbols is part of the 'quantmod' package.

library(quantmod)

# Using getSymbols to import the ETF price histories will take a minute or two or 
# five - 42 time series is a lot of data. 

invisible(getSymbols(etf_ticker_country$ticker, auto.assign = TRUE, warnings = FALSE))

# Let select just the closing prices of the ETFs and merge them into a list.
# We'll use lapply for that purpose. Again, this is for testing purposes. It's not 
# going into production in our app.

etf_prices <- do.call(merge, lapply(etf_ticker_country$ticker, function(x) Cl(get(x))))

#Change the column names to the country names from our dataframe above.

colnames(etf_prices) <- etf_ticker_country$name

# Take a peek at the last 5 rows of each of the time series, 
# just to make sure it looks complete.

tail(etf_prices, n = 5)
##            Japan Brazil India China Germany Canada Korea Taiwan
## 2016-12-07 50.52  33.28 27.25 37.69   26.16  26.65 54.95  31.26
## 2016-12-08 51.10  33.01 27.55 37.69   26.07  26.86 55.41  31.53
## 2016-12-09 51.25  32.82 27.45 37.47   26.09  26.94 55.00  31.36
## 2016-12-12 50.75  32.43 27.16 36.61   26.11  27.00 55.06  31.19
## 2016-12-13 51.35  32.72 27.36 37.01   26.34  27.17 55.52  31.49
##            United Kingdom Hong Kong Australia Mexico Switzerland Spain
## 2016-12-07          31.09     20.97     21.04  45.00       28.96 26.76
## 2016-12-08          31.03     20.74     21.10  45.55       28.73 26.83
## 2016-12-09          31.18     20.60     21.15  45.84       29.13 26.84
## 2016-12-12          31.05     20.29     21.20  46.21       28.98 26.91
## 2016-12-13          31.34     20.53     21.32  46.26       29.43 27.32
##            Singapore Italy Indonesia Russia Chile South Africa Thailand
## 2016-12-07     22.00 23.79     24.55  32.06 39.20        52.85    73.20
## 2016-12-08     21.83 23.72     24.65  32.82 39.49        53.10    73.20
## 2016-12-09     21.71 23.49     24.54  33.18 39.68        52.80    72.71
## 2016-12-12     21.73 23.59     24.33  34.15 39.39        52.94    72.54
## 2016-12-13     21.83 24.14     24.70  34.84 39.76        54.00    73.37
##            Turkey Sweden France Malaysia  Peru Netherlands Poland
## 2016-12-07  33.60  28.65  24.63    29.91 33.23       24.11  18.41
## 2016-12-08  32.73  28.59  24.37    30.07 33.18       23.92  18.33
## 2016-12-09  32.00  28.78  24.44    30.04 32.97       24.03  18.00
## 2016-12-12  32.54  28.78  24.46    29.99 32.97       24.08  18.02
## 2016-12-13  32.53  29.05  24.66    30.06 33.11       24.26  18.42
##            Philippines New Zealand Ireland Belgium Israel Austria Denmark
## 2016-12-07       32.98       42.88   37.72   17.57  46.88   16.95   49.77
## 2016-12-08       33.46       42.92   37.41   17.45  46.85   16.84   49.09
## 2016-12-09       33.23       42.60   37.55   17.41  46.96   16.63   49.86
## 2016-12-12       32.52       42.67   37.44   17.38  47.13   16.70   49.69
## 2016-12-13       32.71       42.70   37.52   17.50  47.32   16.78   50.14
##            Qatar United Arab Emirates Finland Norway Colombia South Korea
## 2016-12-07 18.35               16.720   32.96  22.30    12.65      23.595
## 2016-12-08 18.43               17.058   32.46  22.37    12.53      23.595
## 2016-12-09 18.43               17.080   32.35  22.34    12.66      23.595
## 2016-12-12 18.49               17.370   32.61  22.47    12.72      24.310
## 2016-12-13 19.17               17.315   32.96  22.57    12.97      24.310
##            Saudi Arabia
## 2016-12-07       25.400
## 2016-12-08       25.369
## 2016-12-09       25.380
## 2016-12-12       25.410
## 2016-12-13       25.351