The following code returns a string called "GLD".
CatItUp <- function(x){
print(x)
}
CatItUp("GLD")
This code returns the tail of GLD prices. But obviously, because I've hard-coded GLD into the function.
IAmMoney <- function(x) {
require("quantmod")
getSymbols("GLD")
tail(GLD)
}
IAmMoney("GLD")
This does not return prices like the hard-coded version, but the "GLD" string like the CatItUp() example above. I don't know why.
IAmMoney <- function(x) {
require("quantmod")
getSymbols("x")
tail(x)
}
IAmMoney("GLD")
How can you pass 'GLD' to the quantmod::getSymbols function, inside the IAmMoney() function?
Aren't you simply overlooking the fact that getSymbols()
has an option auto.assign
?
So you may want this instead:
R> library(quantmod)
R> silly <- function(sym) {
+ x <- getSymbols(sym, auto.assign=FALSE)
+ tail(x)
+ }
R> silly("IBM")
IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2010-12-03 144.25 145.68 144.25 145.38 3710600 145.38
2010-12-06 144.54 145.87 144.52 144.99 3321800 144.99
2010-12-07 146.02 146.30 143.87 144.02 4828600 144.02
2010-12-08 144.35 145.65 143.84 144.98 4961400 144.98
2010-12-09 145.94 145.94 143.52 144.30 4405300 144.30
2010-12-10 144.88 144.95 143.73 144.82 3503800 144.82
R> silly("C")
C.Open C.High C.Low C.Close C.Volume C.Adjusted
2010-12-03 4.38 4.46 4.35 4.45 360277300 4.45
2010-12-06 4.45 4.50 4.43 4.45 445170000 4.45
2010-12-07 4.55 4.65 4.54 4.62 3265796000 4.62
2010-12-08 4.61 4.64 4.55 4.64 913820900 4.64
2010-12-09 4.68 4.71 4.64 4.69 731119000 4.69
2010-12-10 4.70 4.77 4.66 4.77 763156100 4.77
R>
getSymbols()
default behaviour of "I will stick it into your environment as a new variable" is more or less a design flaw and, as I recall, recognised as such.
And hence the behaviour can be altered by auto.assign
.