R/finTables.R

#' Fetch Financial Tables
#'
#' Fetches financial tables from source.
#'
#' @param symbol A character value that you can use ticker.
#' @param freq A character value that set of tables' frequency. One of "\bold{annual}"and"\bold{quarter}".
#' @param type A character value or NULL(is meaning balance-sheet).One of "\bold{balance-sheet}"and"\bold{income}", "\bold{cash-flow}".
#' @return A data.frame is that contains according type of balance sheet tables, income tables, cash-flow tables.
#' @example ##NOT RUN:
#' barData("THYAO")
#' barData("THYAO",startDate = "06-13-2016","06-15-2016")
#' @seealso \code{\link{fBIST},\link{barData}}
#' @export
finTables <- function(symbol,freq,type=NULL) {


  if(is.null(type)) {
    ##GET HTML
    link<- paste0("http://www.marketwatch.com/investing/stock/",symbol,"/financials/balance-sheet/",freq)
    ## PARSE HTML
    meta <- readHTMLTable(link)
    ## Clean meta[1] names to rbind meta.
    names(meta[[1]])[1] <- ""               ## meta is still list.
    dframe <- do.call(rbind, meta)          ## merge data.frame .
    dframe <- dframe[,-7]                   ## delete seventh column.
    rowValue <- nrow(dframe)                ## get nrow
    `rownames<-`(dframe, 1:rowValue)        ## get data frame with row names.

  }
  else if(type=="income") {
    # income statement

    link <- paste0("http://www.marketwatch.com/investing/stock/",symbol,"/financials/",type,"/",freq)
    meta <- readHTMLTable(link)
    names(meta[[1]])[1] <- ""
    dframe <- do.call(rbind, meta)
    dframe <- dframe[,-7]
    rowValue <- nrow(dframe)
    `rownames<-`(dframe, 1:rowValue)


  }
  else if(type=="cash-flow") {

    link <- paste0("http://www.marketwatch.com/investing/stock/",symbol,"/financials/",type,"/",freq)
    meta <- readHTMLTable(link)
    names(meta[[1]])[1] <- ""
    dframe <- do.call(rbind, meta)
    dframe <- dframe[,-7]
    rowValue <- nrow(dframe)
    `rownames<-`(dframe, 1:rowValue)


  }
  else if(type=="balance-sheet") {

    link <- paste0("http://www.marketwatch.com/investing/stock/",symbol,"/financials/",type,"/",freq)
    meta <- readHTMLTable(link)
    names(meta[[1]])[1] <- ""
    dframe <- do.call(rbind, meta)
    dframe <- dframe[,-7]
    rowValue <- nrow(dframe)
    `rownames<-`(dframe, 1:rowValue)

  }
  else {
    stop(paste0("Unsupported type. Please try 'income','cash-flow' or 'balance-sheet'"))
  }
}
enesn/rBIST documentation built on May 16, 2019, 5:12 a.m.