read table

we will use the camera data set

cameraData<-read.table("./data/cameras.csv", sep=",", header=TRUE)
head(cameraData)
##                          address direction      street  crossStreet
## 1       S CATON AVE & BENSON AVE       N/B   Caton Ave   Benson Ave
## 2       S CATON AVE & BENSON AVE       S/B   Caton Ave   Benson Ave
## 3 WILKENS AVE & PINE HEIGHTS AVE       E/B Wilkens Ave Pine Heights
## 4        THE ALAMEDA & E 33RD ST       S/B The Alameda      33rd St
## 5        E 33RD ST & THE ALAMEDA       E/B      E 33rd  The Alameda
## 6        ERDMAN AVE & N MACON ST       E/B      Erdman     Macon St
##                 intersection                      Location.1
## 1     Caton Ave & Benson Ave (39.2693779962, -76.6688185297)
## 2     Caton Ave & Benson Ave (39.2693157898, -76.6689698176)
## 3 Wilkens Ave & Pine Heights  (39.2720252302, -76.676960806)
## 4     The Alameda  & 33rd St (39.3285013141, -76.5953545714)
## 5      E 33rd  & The Alameda (39.3283410623, -76.5953594625)
## 6         Erdman  & Macon St (39.3068045671, -76.5593167803)

note that read.csv() can be read also

read xls

fileUrl<-"https://data.baltimorecity.gov/api/views/dz54-2aru/rows.xlsx?accessType=DOWNLOAD"
#download.file(fileUrl, destfile="./data/cameras.xlsx")
dateDownloaded<-date()

have the right library

#install.packages("xlsx")
library(xlsx)
## Warning: package 'xlsx' was built under R version 3.1.2
## Loading required package: rJava
## Warning: package 'rJava' was built under R version 3.1.2
## Loading required package: xlsxjars
## Warning: package 'xlsxjars' was built under R version 3.1.2
cameraData<-read.xlsx("./data/cameras.xlsx", sheetIndex=1, header=TRUE)

read XML

# library(XML)
# fileUrl<-"http://www.w3schools.com/xml/simple.xml"
# doc<-xmlTreeParse(fileUrl, useInternal=TRUE)
# rootNode<-xmlRoot(doc)
# xmlNode(rootNode)
# names(rootNode)
# rootNode[[1]]
# rootNode[[1]][[1]]
# xmlSApply(rootNode,xmlValue)

let us see how would you go insite each node

# xpathSApply(rootNode, "//name", xmlValue)
# xpathSApply(rootNode, "//price",xmlValue)

let us see how to parse an html

library(XML)
## Warning: package 'XML' was built under R version 3.1.2
fileUrl<-"http://espn.go.com/nfl/team/_/name/bal/baltimore-ravens"
doc<-htmlTreeParse(fileUrl, useInternal=TRUE)
scores<-xpathSApply(doc, "//li[@class='score']",xmlValue)
teams<-xpathSApply(doc, "//li[@class='team-name']", xmlValue)
scores
## [1] "23-16" "26-6"  "23-21" "38-10" "20-13" "48-17" "29-7"  "27-24" "43-23"
teams
##  [1] "Cincinnati"   "Pittsburgh"   "Cleveland"    "Carolina"    
##  [5] "Indianapolis" "Tampa Bay"    "Atlanta"      "Cincinnati"  
##  [9] "Pittsburgh"   "Tennessee"    "New Orleans"  "San Diego"   
## [13] "Miami"        "Jacksonville" "Houston"      "Cleveland"