Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a URL and read Data with R

Tags:

url

r

Is there a way I can specify and get data from a web site URL on to a CSV file for analysis using R?

like image 733
user597551 Avatar asked Jun 09 '11 21:06

user597551


People also ask

How do I import data from URL into R?

To import data from a web site, first obtain the URL of the data file. Click on the “Import Dataset” tab in Rstudio and paste the URL into the dialog box. Then click “OK”. After you hit “OK” you will get another dialog box.

How do I read a CSV file from URL in R?

Method 1: Using Base Rcsv() methods, which is an inbuilt function in R programming. This function is similar to Python because it read the CSV file and returns the data into dataframe.

How do you access data in RStudio?

In RStudio, click on the Workspace tab, and then on “Import Dataset” -> “From text file”. A file browser will open up, locate the . csv file and click Open. You'll see a dialog that gives you a few options on the import.


1 Answers

In the simplest case, just do

X <- read.csv(url("http://some.where.net/data/foo.csv")) 

plus which ever options read.csv() may need.

Edit in Sep 2020 or 9 years later:

For a few years now R also supports directly passing the URL to read.csv:

X <- read.csv("http://some.where.net/data/foo.csv") 

End of 2020 edit. Original post continutes.

Long answer: Yes this can be done and many packages have use that feature for years. E.g. the tseries packages uses exactly this feature to download stock prices from Yahoo! for almost a decade:

R> library(tseries) Loading required package: quadprog Loading required package: zoo      ‘tseries’ version: 0.10-24      ‘tseries’ is a package for time series analysis and computational finance.      See ‘library(help="tseries")’ for details.  R> get.hist.quote("IBM") trying URL 'http://chart.yahoo.com/table.csv?    ## manual linebreak here   s=IBM&a=0&b=02&c=1991&d=5&e=08&f=2011&g=d&q=q&y=0&z=IBM&x=.csv' Content type 'text/csv' length unknown opened URL .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... ........ downloaded 258 Kb               Open   High    Low  Close 1991-01-02 112.87 113.75 112.12 112.12 1991-01-03 112.37 113.87 112.25 112.50 1991-01-04 112.75 113.00 111.87 112.12 1991-01-07 111.37 111.87 110.00 110.25 1991-01-08 110.37 110.37 108.75 109.00 1991-01-09 109.75 110.75 106.75 106.87 [...] 

This is all exceedingly well documented in the manual pages for help(connection) and help(url). Also see the manul on 'Data Import/Export' that came with R.

like image 116
Dirk Eddelbuettel Avatar answered Sep 22 '22 08:09

Dirk Eddelbuettel