如何使用R从互联网下载文件?
在本文中,我们将研究使用 R 编程语言从 Internet 下载任何类型文件的方法。要从 Internet 下载任何类型的文件,使用download.file()函数。此函数可用于从 Internet 下载文件。
Syntax: download.file(url, destfile, method, quiet = FALSE, mode = “w”, cacheOK = TRUE, extra = getOption(“download.file.extra”),headers = NULL, …)
Parameters:
- URL:-a character string (or longer vector e.g., for the “libcurl” method) naming the URL of a resource to be downloaded.
- destfile:-a character string (or vector, see URL) with the name where the downloaded file is saved.
- method:-Method to be used for downloading files.
Returns: It will save the downloaded file to the provided destination.
在这种使用 R 语言中的 download.file()函数下载任何类型文件的方法中,用户只需要调用 download.file()函数,该函数是 R 语言的内置函数之一,并传递文件的 URL需要下载的文件和保存下载文件的目标地址。
在此示例中,我们将简单地将 URL 变量分配给来自 Internet 的示例 CSV 文件的链接,并为下载的文件广告设置目标地址,然后调用 download.file()函数并将 URL 和目标地址作为参数。
要下载的文件: Sample-Spreadsheet-1000-rows
程序:
R
# Specify URL where file is stored
url <- "https://sample-videos.com/csv/Sample-Spreadsheet-1000-rows.csv"
destination<- "C:/Users/Geetansh Sahni/Documents/downloaded_gfg.csv"
download.file(url, destination)
R
# Specify URL where file is stored
url <- "https://cin.ufpe.br/~fbma/Crack/Cracking%20the%20Coding%
20Interview%20189%20Programming%20Questions%20
and%20Solutions.pdf"
destination<- "C:/Users/Geetansh Sahni/Documents/downloaded_gfg.pdf"
download.file(url, destination)
输出:
在此示例中,我们将简单地将 URL 变量分配给来自 Internet 的示例 PDF 文件的链接,并设置下载文件广告的目标地址,然后调用 download.file()函数并将 URL 和目标地址作为参数。
下载文件: sample
程序:
电阻
# Specify URL where file is stored
url <- "https://cin.ufpe.br/~fbma/Crack/Cracking%20the%20Coding%
20Interview%20189%20Programming%20Questions%20
and%20Solutions.pdf"
destination<- "C:/Users/Geetansh Sahni/Documents/downloaded_gfg.pdf"
download.file(url, destination)
输出: