I want to know if we can reach html tables using readr package with the url of the page where html table is published. For example, I want to import table on the page to be loaded into R.
Asked
Active
Viewed 301 times
-3
-
5If you're not leaving Hadleyland, you want rvest. But `XML::readHTMLTable()` will read that baby like a chizamp. – Rich Scriven May 24 '16 at 20:18
-
1Did you read [the description](https://cran.r-project.org/web/packages/readr/index.html)? *"Read flat/tabular text files from disk (or a connection)."* – Jaap May 24 '16 at 20:18
1 Answers
5
I'm not sure what a 'chizamp' might be, but Scriven is correct, albeit with a bit of extra noodling needed to figure out which of the multiple tables returned by XML::readHTMLTables
might be the one you wanted:
> library(XML)
> help(pack=XML)
> sports <- readHTMLTable("http://sports.yahoo.com/nfl/stats/byteam?group=Offense&cat=Total&conference=NFL&year=season_2010&sort=530&old_category=Total&old_group=Offense")
> str(sports[[1]])
'data.frame': 1 obs. of 2 variables:
$ V1: Factor w/ 1 level "": 1
$ V2: Factor w/ 1 level "Search SportsSearch Web": 1
> str(sports[[2]])
'data.frame': 2 obs. of 1 variable:
$ : Factor w/ 2 levels "","Sortable Stats": 2 1
> length(sports)
[1] 8
> str(sports[[8]])
NULL
# top of str() on the seventh item in that list
str(sports[[7]])
'data.frame': 32 obs. of 28 variables:
$ Team : Factor w/ 32 levels "Arizona Cardinals",..: 19 26 24 14 2 23 21 9 13 12 ...
$ : Factor w/ 1 level "": 1 1 1 1 1 1 1 1 1 1 ...
$ G : Factor w/ 1 level "16": 1 1 1 1 1 1 1 1 1 1 ...
$ : Factor w/ 1 level "": 1 1 1 1 1 1 1 1 1 1 ...
$ Pts/G : Factor w/ 28 levels "12.3","16.9",..: 28 27 26 25 24 23 22 22 21 20 ...

IRTFM
- 258,963
- 21
- 364
- 487