R Quantmod获取/查看循环中的财务信息

R Quantmod获取/查看循环中的财务信息,r,financial,quantmod,R,Financial,Quantmod,我想循环浏览股票行情表,获取它们的财务信息,并将它们导出到我桌面上文件夹中的CSV文件中。但是,我遇到了与Quantmod包中viewFinancials()相关的R错误。代码和错误如下所示 所以,我的问题是如何分配一个变量作为financial类的对象,这样我的循环才能正常运行?或者,如果有人有其他选择,我会很高兴听到它 以下是错误消息: viewFinancials中的错误(co.f,“BS”,“Q”): “x”必须是“财务”类型 以下是我正在编写的代码: tickers <- c(

我想循环浏览股票行情表,获取它们的财务信息,并将它们导出到我桌面上文件夹中的CSV文件中。但是,我遇到了与Quantmod包中viewFinancials()相关的R错误。代码和错误如下所示

所以,我的问题是如何分配一个变量作为financial类的对象,这样我的循环才能正常运行?或者,如果有人有其他选择,我会很高兴听到它

以下是错误消息:

viewFinancials中的错误(co.f,“BS”,“Q”): “x”必须是“财务”类型

以下是我正在编写的代码:

 tickers <- c('AAPL','ORCL','MSFT')

 for(i in 1:length(tickers)){

     co <- tickers[1]
     #co.f <- paste(co,".f",sep='') #First attempt, was worth a try

     co.f <- getFin(co, auto.assign=T)  # automatically assigns data to "co.f" object
     BS.q<-viewFinancials(co.f,'BS',"Q")  # quarterly balance sheet
     IS.q<-viewFinancials(co.f,"IS","Q")  # quarterly income statement
     CF.q<-viewFinancials(co.f,"CF","Q")  # quarterly cash flow statement
     BS<-viewFinancials(co.f,"BS","A")  # annual balance sheet
     IS<-viewFinancials(co.f,"IS","A")  # annual income statement
     CF<-viewFinancials(co.f,"CF","A")  # annual cash flow statement

     d<-Sys.Date()

     combinedA <- rbind(BS,IS,CF)
     combinedQ <- rbind(BS.q,IS.q,CF.q)

     BSAfile <- paste('/Users/dedwards/Desktop/RFinancials/',d,' ',co,'_BS_A.csv',sep='')
     BSQfile <- paste('/Users/dedwards/Desktop/RFinancials/',d,' ',co,'_BS_Q.csv',sep='')
     write.csv(combinedA, file = BSAfile, row.names=TRUE)
     write.csv(combinedQ, file = BSQfile, row.names=TRUE)
}

tickers
co.f
包含工作区中实际包含financials对象的对象的名称。要实际使用该对象,需要调用
get(co.f)


<代码> Obj> p>而不是编写一个循环,您可以考虑<代码> TyDyQualt包,它可以将多个股票传递给<代码> TQJGET()/<代码>函数。设置
tq\u get(get=“financials”)
将允许您下载多支股票的财务数据。下面是一个例子:

library(tidyquant)
c("FB", "AMZN", "NFLX", "GOOG") %>%
    tq_get(get = "financials")
这将返回年度和季度期间所有财务报表数据(损益表、资产负债表、现金流)的嵌套数据框。您可以使用
unnest()
函数剥离层


如果需要保存数据,可以使用
write\u csv()
函数取消测试然后写入csv

如果要将数据分配给
co.f
,则必须使用
auto.assign=FALSE
co.f <- getFin(co, auto.assign = FALSE)
library(tidyquant)
c("FB", "AMZN", "NFLX", "GOOG") %>%
    tq_get(get = "financials")