Search code examples
rquantmod

importing financial statements from getFin() to data.frame or data.table?


the getFin() function returns an object of type "financials". which contains a list of lists.

getFin("AAPL")

structure of resulting object

i need to create tables for each of the following:

  • Balance Sheet
  • Income Statement
  • Cash Flow

End goal is to display these tables on a dashboard.
Here's what I tried, but it doesn't seem right:

df <- data.frame(AAPL.f[[2]][2])
df2 <- data.frame(viewFin(AAPL.f,"BS", "A"))

How can I get the above statements into Data frames?


Solution

  • This should give you what you want.

    require(quantmod)
    setwd("C:/Users/your_path_here/downloads")
    stocks <- c("AXP","BA","CAT","CSCO","CVX","DD","DIS","GE","GS","HD","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MRK","MSFT","NKE","PFE","PG","T","TRV","UNH","UTX","V","VZ","WMT","XOM")
    
    # equityList <- read.csv("EquityList.csv", header = FALSE, stringsAsFactors = FALSE)
    # names(equityList) <- c ("Ticker")
    
    for (i in 1 : length(stocks)) {   
            temp<-getFinancials(stocks[i],src="google",auto.assign=FALSE)
            write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Annual).csv",sep=""))
            write.csv(temp$BS$A,paste(stocks[i],"_Balance_Sheet(Annual).csv",sep=""))
            write.csv(temp$CF$A,paste(stocks[i],"_Cash_Flow(Annual).csv",sep=""))
            write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Quarterly).csv",sep=""))
            write.csv(temp$BS$A,paste(stocks[i],"_Balance_Sheet(Quaterly).csv",sep=""))
            write.csv(temp$CF$A,paste(stocks[i],"_Cash_Flow(Quaterly).csv",sep=""))
    }