Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify getSymbols.SQLite #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ function(Symbols,env,return.class='xts',

# getSymbols.SQLite {{{
"getSymbols.SQLite" <- function(Symbols,env,return.class='xts',
Date_col='row_names',
db.fields=c('row_names','Open','High',
'Low','Close','Volume','Adjusted'),
field.names = NULL,
col.names = NULL,
dbname=NULL,
POSIX = TRUE,
...) {
Expand Down Expand Up @@ -567,7 +568,7 @@ function(Symbols,env,return.class='xts',
query <- paste("SELECT ",
paste(db.fields,collapse=','),
" FROM ",Symbols[[i]],
" ORDER BY row_names")
" ORDER BY ", Date_col)
rs <- DBI::dbSendQuery(con, query)
fr <- DBI::fetch(rs, n=-1)
#fr <- data.frame(fr[,-1],row.names=fr[,1])
Expand All @@ -578,9 +579,12 @@ function(Symbols,env,return.class='xts',
} else {
fr <- xts(fr[,-1],order.by=as.Date(as.numeric(fr[,1]),origin='1970-01-01'))
}
colnames(fr) <- paste(Symbols[[i]],
c('Open','High','Low','Close','Volume','Adjusted'),
sep='.')
if (!is.null(col.names)){
colnames(fr) <- paste(Symbols[[i]],col.names , sep = ".")
}
else {
colnames(fr) <- paste(Symbols[[i]],db.fields[db.fields!=Date_col] , sep = ".")
}
fr <- convert.time.series(fr=fr,return.class=return.class)
if(auto.assign)
assign(Symbols[[i]],fr,env)
Expand All @@ -595,9 +599,9 @@ function(Symbols,env,return.class='xts',
# }}}

# getSymbols.MySQL {{{
"getSymbols.MySQL" <- function(Symbols,env,return.class='xts',
"getSymbols.MySQL" <- function(Symbols,env,return.class='xts',Date_col="date",
db.fields=c('date','o','h','l','c','v','a'),
field.names = NULL,
col.names = NULL,
user=NULL,password=NULL,dbname=NULL,host='localhost',port=3306,
...) {
importDefaults("getSymbols.MySQL")
Expand Down Expand Up @@ -631,16 +635,19 @@ function(Symbols,env,return.class='xts',
if(verbose) {
cat(paste('Loading ',Symbols[[i]],paste(rep('.',10-nchar(Symbols[[i]])),collapse=''),sep=''))
}
query <- paste("SELECT ",paste(db.fields,collapse=',')," FROM ",Symbols[[i]]," ORDER BY date")
query <- paste("SELECT ",paste(db.fields,collapse=',')," FROM ",Symbols[[i]]," ORDER BY ",Date_col)
rs <- DBI::dbSendQuery(con, query)
fr <- DBI::fetch(rs, n=-1)
#fr <- data.frame(fr[,-1],row.names=fr[,1])
fr <- xts(as.matrix(fr[,-1]),
order.by=as.Date(fr[,1],origin='1970-01-01'),
src=dbname,updated=Sys.time())
colnames(fr) <- paste(Symbols[[i]],
c('Open','High','Low','Close','Volume','Adjusted'),
sep='.')
if (!is.null(col.names)){
colnames(fr) <- paste(Symbols[[i]],col.names , sep = ".")
}
else {
colnames(fr) <- paste(Symbols[[i]],db.fields[db.fields!=Date_col] , sep = ".")
}
fr <- convert.time.series(fr=fr,return.class=return.class)
if(auto.assign)
assign(Symbols[[i]],fr,env)
Expand Down
6 changes: 4 additions & 2 deletions man/getSymbols.MySQL.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ further user contributed data tools.
getSymbols.MySQL(Symbols,
env,
return.class = 'xts',
Date_col="date",
db.fields = c("date", "o", "h", "l", "c", "v", "a"),
field.names = NULL,
col.names = NULL,
user = NULL,
password = NULL,
dbname = NULL,
Expand All @@ -31,9 +32,10 @@ getSymbols.MySQL(Symbols,
\item{return.class}{ desired class of returned object.
Can be xts,
zoo, data.frame, ts, or its. (zoo)}
\item{Date_col}{ column name of date in database }
\item{db.fields}{ character vector indicating
names of fields to retrieve}
\item{field.names}{ names to assign to returned columns }
\item{col.names}{ names to assign to returned columns }
\item{user}{ username to access database }
\item{password}{ password to access database }
\item{dbname}{ database name }
Expand Down
6 changes: 4 additions & 2 deletions man/getSymbols.SQLite.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ this function should \emph{NOT} be called directly.
getSymbols.SQLite(Symbols,
env,
return.class = 'xts',
Date_col="row_names",
db.fields = c("row_names",
"Open",
"High",
"Low",
"Close",
"Volume",
"Adjusted"),
field.names = NULL,
col.names = NULL,
dbname = NULL,
POSIX = TRUE,
...)
Expand All @@ -27,8 +28,9 @@ getSymbols.SQLite(Symbols,
symbol to be loaded }
\item{env}{ where to create the objects }
\item{return.class}{ desired class of returned object }
\item{Date_col}{ column name of date in database }
\item{db.fields}{ character vector naming fields to retrieve }
\item{field.names}{ names to assign to returned columns }
\item{col.names}{ names to assign to returned columns }
\item{dbname}{ database name }
\item{POSIX}{ are rownames numeric }
\item{\dots}{ additional arguments }
Expand Down