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

1. Fixed single column matrix acccidentally being converted down to a vector. 2. Added feature keepHTMLTITLEtext #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 41 additions & 5 deletions R/fundamental.data.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ fund.data <- function
Symbol, # ticker
n=10, # number of periods
mode=c('quarterly','annual'), # periodicity
max.attempts=5 # maximum number of attempts to download before exiting
max.attempts=5, # maximum number of attempts to download before exiting
keepHTMLTITLEtext = FALSE # last row includes HTML TITLE text
)
{
all.data = c()
Expand Down Expand Up @@ -70,7 +71,15 @@ if( len(grep('INDICATORS', txt, ignore.case = T)) == 0 ) {
cat('No Data Found for', Symbol, '\n')
return(all.data)
}


if ( keepHTMLTITLEtext == TRUE ) {
# extract title from this page
HTMLOPENTITLETAGposStart <- regexpr(pattern="<title>", txt,ignore.case=TRUE)[1]
HTMLCLOSETITLETAGposStart <- regexpr(pattern="</title>",txt,ignore.case=TRUE)[1]
HTMLOPENTITLETAGlength <- nchar("<title>")
HTMLTITLEtext <- substr(txt, HTMLOPENTITLETAGposStart + HTMLOPENTITLETAGlength , HTMLCLOSETITLETAGposStart - 1 )
}

# extract table from this page
data = extract.table.from.webpage(txt, 'INDICATORS', hasHeader = T)
colnames(data) = data[1,]
Expand Down Expand Up @@ -107,16 +116,43 @@ if( len(grep('INDICATORS', txt, ignore.case = T)) == 0 ) {

# remove empty columns
all.data = all.data[, colSums(nchar(trim(all.data))) > 0]
# if converted to a vector, then make it a matrix again
if (is.vector(all.data)) {
all.data.temp <- matrix(all.data,nrow=length(all.data))
rownames(all.data.temp) <- names(all.data)
colnames(all.data.temp) <- all.data.temp[1, ]
all.data <- all.data.temp
all.data.temp <- matrix(nrow=0, ncol=0)
}

if( ncol(all.data) > n ) {
return(all.data[,(ncol(all.data)-n+1):ncol(all.data)])
all.data <- all.data[, (ncol(all.data) - n + 1):ncol(all.data)]
# if converted to a vector, then make it a matrix again
if (is.vector(all.data)) {
all.data.temp <- matrix(all.data,nrow=length(all.data))
rownames(all.data.temp) <- names(all.data)
colnames(all.data.temp) <- all.data.temp[1, ]
all.data <- all.data.temp
all.data.temp <- matrix(nrow=0, ncol=0)
}
if ( keepHTMLTITLEtext == TRUE ) {
# add a row of the HTMLTITLEtext values
all.data <- rbind(all.data, rep( HTMLTITLEtext, ncol(all.data) ) )
# to the new 'added row' name it "HTMLTITLEtext"
rownames(all.data)[nrow(all.data)] <- "HTMLTITLEtext"
}
return(all.data)
} else {
if ( keepHTMLTITLEtext == TRUE ) {
# add a row of the HTMLTITLEtext values
all.data <- rbind(all.data, rep( HTMLTITLEtext, ncol(all.data) ) )
# to the new 'added row' name it "HTMLTITLEtext"
rownames(all.data)[nrow(all.data)] <- "HTMLTITLEtext"
}
return(all.data)
}
}



###############################################################################
# determine date when fundamental data is available
# use 'date preliminary data loaded' when available
Expand Down