使用R从ftp下载文件 [英] Downloading files from ftp with R

查看:187
本文介绍了使用R从ftp下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此FTP获取文件

I am trying to get files from this FTP

ftp://ftp.pride.ebi .ac.uk/pride/data/archive/2015/11/PXD000299/

从那里,我仅需要以.dat扩展名开头的文件.但是还有其他我不感兴趣的文件.

From there, I need only the files starting with the .dat extension. But there are other files that I am not interested in.

我想避免一次下载每个,所以我想创建一个带有名称的矢量并循环遍历它们.

I want to avoid downloading each one at a time, so I thought in creating a vector with the names and loop over them.

如何只下载我想要的文件?

How can I download only the files I want?

谢谢

我已经尝试过以下操作

I have tried doing the following

downloadURL <- "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat"
download.file(downloadURL, "F010439.dat") #this is a trial using one file

几秒钟后,出现以下错误:

And after a few seconds I get the following error:

trying URL 

    'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat'
    Error in download.file(downloadURL, "F010439.dat") : 
      cannot open URL 'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat'
    In addition: Warning message:
    In download.file(downloadURL, "F010439.dat") :
      InternetOpenUrl failed: 'Die FTP-Sitzung wurde beendet.

'

推荐答案

使用curl库提取目录列表

Use the curl library to extract the directory listing

> library(curl)
> url = "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/"
> h = new_handle(dirlistonly=TRUE)
> con = curl(url, "r", h)
> tbl = read.table(con, stringsAsFactors=TRUE, fill=TRUE)
> close(con)
> head(tbl)
                                                 V1
1  12-0210_Druart_Uterus_J0N-Co_1a_ORBI856.raw.mzML
2  12-0210_Druart_Uterus_J0N-Co_2a_ORBI857.raw.mzML
3  12-0210_Druart_Uterus_J0N-Co_3a_ORBI858.raw.mzML
4 12-0210_Druart_Uterus_J10N-Co_1a_ORBI859.raw.mzML
5 12-0210_Druart_Uterus_J10N-Co_2a_ORBI860.raw.mzML
6 12-0210_Druart_Uterus_J10N-Co_3a_ORBI861.raw.mzML

将相关内容粘贴到网址上并使用

Paste the relevant ones on to the url and use

urls <- paste0(url, tbl[1:5,1])
fls = basename(urls)
curl_fetch_disk(urls[1], fls[1])

这篇关于使用R从ftp下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆