1. Setting default arguments for getSymbols()
You've learned how getSymbols() provides access to several data sources, and that getSymbols() imports data from Yahoo Finance by default. What if your analysis depends on data primarily from a source other than Yahoo Finance? Do you have to specify "src" for every getSymbols() call?
In this video, you will learn how to use the setDefaults() function to set a new default data source for getSymbols(), and how you can use setDefaults() to customize default arguments passed to the underlying getSymbols() methods.
2. getSymbols() “methods”
What are getSymbols() methods? The getSymbols() function doesn't actually contain code to import data. Instead, it determines the data source for each symbol, and then calls the getSymbols() "method" to do the actual import. For example, when you import GDP data from FRED, you call getSymbols() with "src" set to "FRED". Then getSymbols() calls the getSymbols-dot-FRED() function. Note that you should not call these "methods" directly.
3. Use setDefaults() to change default data source
Let's look at an example of how to use setDefaults(). Suppose you want to change the default data source from Yahoo Finance to FRED. The first argument to setDefaults() is the getSymbols() function. The rest of the arguments to setDefaults() are pairs of argument names and new default values. In this case, you just need to set "src" to "FRED". That's it! Now if you call getSymbols() without a "src" argument, it will import data from FRED.
4. setDefaults()
A few important things to keep in mind about setDefaults(). You must specify arguments as name equals value pairs. setDefaults() only works with getSymbols() and getSymbols() methods, because they actively check to see if you have specified different default values. All the defaults you set are stored in R's global options(), which means they do not persist across R sessions. Using global options may make your scripts harder for others to reproduce, unless you are careful to include your default settings in your scripts.
5. Other arguments
Now let's change a default for a getSymbols() method. Suppose you would like to import historical data from Yahoo Finance, but only want data for 2016. First, you need to find the formal arguments to getSymbols-dot-yahoo(). You can do that using the args() function to print them to the screen, or you can use the help() function to read the documentation.
6. Default from and to values
If you use the args() function, you can see that getSymbols-dot-yahoo() has "from" and "to" arguments. If you only want data for 2016, you need to set the "from" argument to January 1, 2016 and you need set the "to" argument to December 31, 2016. Again, the first argument to setDefaults() is the function you want to change the defaults for, getSymbols-dot-yahoo() in this case.
7. getDefaults()
You can use the getDefaults() function to see the default values that have been set for various functions. If you call getDefaults() without any arguments, it will return a list of the functions that have user-specified defaults. If you then call getDefaults() with the name of one of those functions, it will show you user-specified default argument names and values. Although you can try to set defaults for any function, remember that it only works if the function knows to check for user-specified defaults.
8. Let's practice!
Now it's your turn to practice customizing your getSymbols() experience with setDefaults().