[R-SIG-Finance] yahooImport
Joe Byers
ecjbosu at aol.com
Tue Feb 13 20:37:53 CET 2007
I am trying to use the yahooImport() and get the following error for the
default source as
yahooImport('IBM')
trying URL 'http://chart.yahoo.com/table.csv?IBM'
Error in download.file(url = paste(source, query, sep = ""), destfile =
file, :
cannot open URL 'http://chart.yahoo.com/table.csv?IBM'
In addition: Warning message:
cannot open: HTTP status was '404 Not Found'
Error: attempt to apply non-function
Yahoo has changed the source URL to
http://ichart.finance.yahoo.com/table.csv?s=IBM
so the default might need changing as well.
I tried passing the new source as
a<-yahooImport('IBM','source=http://ichart.finance.yahoo.com/table.csv?s=')
I had to add s= to the source string. I now get the following error
trying URL 'http://ichart.finance.yahoo.com/table.csv?s=IBM'
Content type 'text/csv' length unknown
opened URL
downloaded 567Kb
Read 11356 items
Error in matrix(unlist(x2), byrow = TRUE, nrow = length(x2)) :
matrix: invalid 'ncol' value (< 0)
a now has the 'no internet access' as its value.
I am stuck here. Any thoughts?
I included yahooImport below for reference
Thank you
Joe
function (query, file = "tempfile", source =
"http://chart.yahoo.com/table.csv?",
save = FALSE, sep = ";", swap = 20, try = TRUE)
{
if (try) {
z = try(yahooImport(file = file, source = source, query = query,
save = save, try = FALSE))
if (class(z) == "try-error" || class(z) == "Error") {
return("No Internet Access")
}
else {
return(z)
}
}
else {
if (class(version) != "Sversion") {
method = NULL
}
else {
method = "wget"
}
download.file(url = paste(source, query, sep = ""), destfile =
file,
method = method)
x1 = rev(scan(file, what = "", skip = 1))
x2 = strsplit(x1[regexpr("-...-..,", x1) > 0], ",")
x1 = matrix(unlist(x2), byrow = TRUE, nrow = length(x2))
z = matrix(as.numeric(x1[, -1]), ncol = dim(x1)[2] -
1)
rowNames = as.character(as.Date(x1[, 1], format = "%d-%b-%y"))
colNames = scan(file = file, n = dim(x1)[2], what = "",
sep = ",")[-1]
dimnames(z) = list(rowNames, colNames)
if (save) {
write.table(t(c("%Y-%m-%d", colNames)), file, quote = FALSE,
row.names = FALSE, col.names = FALSE, sep = ";")
write.table(z, file, quote = FALSE, append = TRUE,
col.names = FALSE, sep = ";")
}
else {
unlink(file)
}
z = data.frame(DATE = rowNames, z, row.names = NULL)
ans = new("fWEBDATA", call = match.call(), param =
c("Instrument Query" = query),
data = z, title = "Web Data Import from Yahoo", description
= as.character(date()))
return(ans)
}
invisible()
}
More information about the R-SIG-Finance
mailing list