在R中的一张图纸中合并多个.xlsx文件

时间:2019-12-18 18:06:26

标签: r

我写了一些代码,可抓取动态Web URL的多个表,并将它们保存在同一文件夹中的不同日期名称的单独文件中。

我需要将所有这些文件分组到一个电子表格中,并将数据互相写在下面。

如果可能的话,我还要添加一个新列(在A列中),该列将在每个单元格的每一行中复制文件日期。

整个代码是:

library("openxlsx")
library('rvest')

start <- as.Date("16-12-19",format="%d-%m-%y")
end   <- as.Date("17-12-19",format="%d-%m-%y")
theDate <- start

while (theDate <= end)
{
  url <- (paste0("http://www.b3.com.br/pt_br/produtos-e-servicos/emprestimo-de-ativos/renda-variavel/emprestimos-registrados/renda-variavel-8AE490CA64CD50310164D1EFD6412F1C.htm?data=",format(theDate,"%d/%m/%y"),"&f=0"))
  site <- read_html(url)
  Info_Ajuste_HTML <- html_nodes(site,'table')


    Info_ajuste <- html_text(Info_Ajuste_HTML)
    head(Info_ajuste,20)
    if (length(Info_Ajuste_HTML) > 0) { ### <- Added a check here
    head(Info_Ajuste_HTML)

  lista_tabela <- site %>%
  html_nodes("table") %>%
  html_table(fill = TRUE) 
  str(lista_tabela)
  head(lista_tabela[[1]], 10)
  AJUSTE <- lista_tabela[[1]]
  write.xlsx(AJUSTE, file=paste0("C:/Users/Jessé/Desktop/R/XLS/",paste0(format(theDate,"%d-%m-%y"),".xlsx")), col.names = (FALSE)) 
  theDate <- theDate + 1 
  }

  else {theDate <- theDate + 1 }

  }

0 个答案:

没有答案