【发布时间】:2011-03-08 01:49:10
【问题描述】:
我想在 R 中执行多步骤文件下载过程。我有中间步骤,但没有第一步和第三步...
# STEP 1 Recursively find all the files at an ftp site
# ftp://prism.oregonstate.edu//pub/prism/pacisl/grids
all_paths <- #### a recursive listing of the ftp path contents??? ####
# STEP 2 Choose all the ones whose filename starts with "hi"
all_files <- sapply(sapply(strsplit(all_paths, "/"), rev), "[", 1)
hawaii_log <- substr(all_files, 1, 2) == "hi"
hi_paths <- all_paths[hawaii_log]
hi_files <- all_files[hawaii_log]
# STEP 3 Download & extract from gz format into a single directory
mapply(download.file, url = hi_paths, destfile = hi_files)
## and now how to extract from gz format?
【问题讨论】:
-
必须是 R 吗? HTTP 充其量是可以通过的,但它在 FTP 上并不是很好。更通用的语言,比如 python,会更适合这类问题。
-
是的,我试图避免添加任何外部工具......现在我已经通过从 R 调用命令行 wget 做了一个解决方法,但我希望能够将它传递给某人作为一个独立的 R 脚本
-
复制和粘贴文本文件名并在循环中使用 download.file 很容易 - 所以它是为您的用户硬编码的,但仍然是独立的(或者您可以通过 ftp 进入站点并mget . . .)
-
您可以使用
dir(pattern = "^hi.+$", ignore.case = TRUE)获取所有“hi”文件。
标签: r