[R-SIG-Finance] Accessing getSymbols data
Joshua Ulrich
josh.m.ulrich at gmail.com
Fri Dec 11 18:07:09 CET 2009
Specifically,
"By default, it is assigned automatically to a variable in the
specified environment, _without_ the user explicitly assigning the
returned data to a variable."
...
"This behavior can be overridden by setting auto.assign to FALSE,
though it is not advised."
So,
table <- getSymbols(yahooSymbol)
does not do what you think it does. If you want to assign the results
of getSymbols() to a variable, you need to set auto.assign=FALSE.
> table <- getSymbols("SPY", auto.assign=FALSE)
> length(table)
[1] 4452
HTH,
Josh
--
http://www.fosstrading.com
On Fri, Dec 11, 2009 at 11:00 AM, Nick Torenvliet
<n_torenvliet at hotmail.com> wrote:
>
> Yeah I got but didn't get that part... the data is loaded in AAB.TO I can access it... e.g.
>
>> AAB.TO[1]
> AAB.TO.Open AAB.TO.High AAB.TO.Low AAB.TO.Close AAB.TO.Volume
> 2007-01-02 0.97 1 0.96 0.96 10500
> AAB.TO.Adjusted
> 2007-01-02 0.96
>
> does the R shell access all the environments by default whilst the R script does not?
>
>> From: josh.m.ulrich at gmail.com
>> Date: Fri, 11 Dec 2009 10:56:34 -0600
>> Subject: Re: [R-SIG-Finance] Accessing getSymbols data
>> To: n_torenvliet at hotmail.com
>> CC: r-sig-finance at stat.math.ethz.ch
>>
>> From ?getSymbols:
>>
>> Details:
>>
>> 'getSymbols' is a wrapper to load data from different sources - be
>> them local or remote. Data is fetched through one of the available
>> 'getSymbols' methods and saved in the 'env' specified - the
>> .GlobalEnv by default. Data is loaded in much the same way that
>> 'load' behaves. By default, it is assigned automatically to a
>> variable in the specified environment, _without_ the user
>> explicitly assigning the returned data to a variable.
>>
>> The previous sentence's point warrants repeating - getSymbols is
>> called for its side effects, and _does not_ return the data object
>> loaded. The data is ‘loaded’ silently by the function into a new
>> environment by default - or the environment specified. This
>> behavior can be overridden by setting auto.assign to FALSE, though
>> it is not advised.
>>
>> Best,
>> Josh
>> --
>> http://www.fosstrading.com
>>
>>
>>
>> On Fri, Dec 11, 2009 at 10:51 AM, Nick Torenvliet
>> <n_torenvliet at hotmail.com> wrote:
>> >
>> > I'm working with the following code:
>> >
>> > atmpt <- try(table <- getSymbols(yahooSymbol))
>> > options(show.error.messages = TRUE)
>> > if(inherits(atmpt, "try-error"){
>> > } else {
>> > # insert data from table here
>> > for (i in 1:length(table)){
>> > sql <- paste("Insert into ", myDBSymbol," (dayDate, dayOpen, dayHigh, dayLow, dayClose, dayVolume, dayAdjustment) values (",index(table[i]),",", table[i,],",", table[i,2],",", table[i,3],",", table[i,4],",", table[i,5],",", symbolData[i,6],")")
>> > atmpt <- try(dbGetQuery( con, sql))
>> > options(show.error.messages = TRUE)
>> > if(inherits(dataEntry, "try-error")){
>> > dataEntry <- NA
>> > } else {
>> > }
>> > }
>> > }
>> >
>> > I imagine you can see a heap of errors in the code above but my first problem is that I'm not accessing the data returned from getSymbols properly and the insert statement fails on bad data.
>> >
>> > AAB.TO happens to be the value of yahooSymbol, from the R prompt I get
>> >
>> >> length(AAB.TO)
>> > [1] 4452
>> >> length(table)
>> > [1] 1
>> >> length(yahooSymbol)
>> > [1] 1
>> >
>> > Accessing AAB.TO is easy enough, but the code is part of a loop where the symbol in yahooSymbol changes each iteration, can anyone help me to access the values returned by getSymbol dynamically as by the insert statement above?
>> >
>> > Regards,
>> >
>> > Nick
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-SIG-Finance at stat.math.ethz.ch mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> > -- Subscriber-posting only.
>> > -- If you want to post, subscribe first.
>> >
>
> [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list