0

I previously had the following code which worked well, it used the quantmod package to download financial information from google. However this functionality has since stopped working. Thankfully somebody has made a work around to getting data from yahoo here.

library(tidyquant)
#symbols <- c("HOG", "GOOG", "GE")
    stockinfo <- tq_index("SP500")
    symbols <- stockinfo$symbol

    library(quantmod)
    symbols.f <- sapply(symbols, function(x) { paste0(x, ".f") })
    symbols <- sub("\\.f","", symbols.f)

    tickers <- new.env()
    lapply(symbols, getFinancials, env=tickers)
    BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
    IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
    CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))

I am applying the following code which incorporates the function to download financial information from yahoo;

#########################################################
#Set the function to scrape Yahoo finance

getFin <- function(stock){
  if ("rvest" %in% installed.packages()) {
    library(rvest)
  }else{
    install.packages("rvest")
    library(rvest)
  }
  for (i in 1:length(stock)) {
    tryCatch(
      {
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/financials?p=",stock[i])
        wahis.session <- html_session(url)                                
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        IS <- p[[1]]
        colnames(IS) <- paste(IS[1,])
        IS <- IS[-c(1,5,12,20,25),]
        names_row <- paste(IS[,1])
        IS <- IS[,-1]
        IS <- apply(IS,2,function(x){gsub(",","",x)})
        IS <- as.data.frame(apply(IS,2,as.numeric))
        rownames(IS) <- paste(names_row)
        temp1 <- IS
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/balance-sheet?p=",stock[i])
        wahis.session <- html_session(url)
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        BS <- p[[1]]
        colnames(BS) <- BS[1,]
        BS <- BS[-c(1,2,17,28),]
        names_row <- BS[,1]
        BS <- BS[,-1] 
        BS <- apply(BS,2,function(x){gsub(",","",x)})
        BS <- as.data.frame(apply(BS,2,as.numeric))
        rownames(BS) <- paste(names_row)
        temp2 <- BS
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/cash-flow?p=",stock[i])
        wahis.session <- html_session(url)
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        CF <- p[[1]]
        colnames(CF) <- CF[1,]
        CF <- CF[-c(1,3,11,16),]
        names_row <- CF[,1]
        CF <- CF[,-1] 
        CF <- apply(CF,2,function(x){gsub(",","",x)})
        CF <- as.data.frame(apply(CF,2,as.numeric))
        rownames(CF) <- paste(names_row)
        temp3 <- CF
        assign(paste0(stock[i],'.f'),value = list(IS = temp1,BS = temp2,CF = temp3),envir = parent.frame())

      },
      error = function(cond){
        message(stock[i], "Give error ",cond)
      }
    )
  }
}

################## END OF FUNCTION ######################
#
#########################################################

symbols <- c("HOG", "GOOG", "GE")

I can use the following getFin(symbols) which download the financial statements. However I am running into an issue as I want to have the data as I had it before, using;

tickers <- new.env()
lapply(symbols, getFin, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))

I run into an error at lapply(symbols, getFin, env=tickers) and not too sure why since I put the data in the same format as before.

EDIT:

To give a sense of what I am trying to achieve (as the end goal). I want 3 separate data frames for BS - Balance Sheet, IS- Income Statement and CF - Cash Flow` with each of the firms listed as a column.

user113156
  • 6,761
  • 5
  • 35
  • 81
  • What is the exact error message you get? – MrFlick Jul 16 '18 at 20:20
  • Apologies, this is the error I am running into `lapply(symbols, getFin, env=tickers) Error in FUN(X[[i]], ...) : unused argument (env = )` – user113156 Jul 16 '18 at 20:22
  • 2
    Well, your `getFin()` function is defined with only one parameter (stock). It doesn't have a parameter named `env=`. You can't pass in a parameter to a function if it doesn't ask for it. – MrFlick Jul 16 '18 at 20:24
  • huh.. so the problem is that `env = tickers` is not defined in the function `getFin`? – user113156 Jul 16 '18 at 20:28
  • Yes, `getFin` only has one parameter and that is `stock`. When you make the call `lapply(symbols, getFin, env=tickers)` then `lapply` tries to pass `tickers` to the `env` argument of `getFin` which does not exist. – Benjamin Christoffersen Jul 16 '18 at 21:45

0 Answers0