Working with web data

R can directly access data stored as .csv files (and/or other formats) from the web. It is as simple as passing the URL to the fread function.

For example, the following loads data directly from the web into flights_extra:

flights_extra <- fread("http://ourairports.com/data/airports.csv")

There are also dedicated packages to work with web pages. One such example is httr as shown as follows:

install.packages("httr") library(httr)rxds <- GET("http://www.rxdatascience.com")rxds# View the HTTP headersheaders(rxds)# View the contentcontent_rxds <- content(rxds, "text")substr(content_rxds,1,500)# Use parsersstr(content(rxds, "parsed"))

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.