输入数据
我们将访问URL天气数据,并使用R下载2015年的CSV文件。
我们将使用getHTMLLinks()函数来收集文件的URL。然后,我们将使用功能download.file()将文件保存到本地系统。由于我们将一次又一次地对多个文件应用相同的代码,因此我们将创建一个被多次调用的函数。文件名以R列表对象的形式作为参数传递给此函数。
# Read the URL.
url <- "http://www.geos.ed.ac.uk/~weather/jcmb_ws/"
# Gather the html links present in the webpage.
links <- getHTMLLinks(url)
# Identify only the links which point to the JCMB 2015 files.
filenames <- links[str_detect(links, "JCMB_2015")]
# Store the file names as a list.
filenames_list <- as.list(filenames)
# Create a function to download the files by passing the URL and filename list.
downloadcsv <- function (mainurl,filename) {
filedetails <- str_c(mainurl,filename)
download.file(filedetails,filename)
}
# Now apply the l_ply function and save the files into the current R working directory.
l_ply(filenames,downloadcsv,mainurl = "http://www.geos.ed.ac.uk/~weather/jcmb_ws/")