r - 从 url 列表中导入 excel 文件
问题描述
我的目标是创建一个直接从 url 导入的数据框列表。我现在拥有的:
library(gdata)
urls <- list('https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-chrom.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-coppe.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-nicke.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-tin.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-tungs.xlsx')
perl_dir <- 'C:/Strawberry/perl/bin/perl5.26.2.exe'
files <- lapply(urls, read.xls, perl = perl_dir)
当我运行它时,我得到了错误:
xls2sep 中的错误(xls,工作表,详细 = 详细,...,方法 = 方法,:中间文件 'C:\Users\Mike\AppData\Local\Temp\Rtmpk9t4hG\file308c4520306c.csv' 丢失!另外:警告消息: 运行命令 '"C:\STRAWB~1\perl\bin\PERL52~1.EXE" "C:/Users/Mike/Documents/R/win-library/3.3/gdata/perl/xls2csv.pl" " https ://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-chrom.xlsx " "C:\Users\Mike\AppData\Local\Temp\Rtmpk9t4hG\file308c4520306c.csv" "1"' 状态为 22 file.exists(tfn) 中的错误:“文件”参数无效
我假设无效的文件参数意味着它找不到文件。不知道如何更正它,因为网址绝对正确。
有任何想法吗?
解决方案
为什么不做这样的事情(使用更强大的readxl
包):
urls <- list('https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-chrom.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-coppe.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-nicke.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-tin.xlsx',
'https://minerals.usgs.gov/minerals/pubs/historical-statistics/ds140-tungs.xlsx')
# Download files
mapply(download.file, unlist(urls), sapply(urls, basename))
# Read Excel files into a list of data.frames
library(readxl);
lst <- lapply(sapply(urls, basename), read_excel);
这会生成文件的本地副本,但如果这不是交易破坏者,我建议使用readxl
. 老实说,保留源数据的副本始终是确保结果的可重复性和一致性的好主意。