04 Data Interfaces in R
04 Data Interfaces in R
Tushar B. Kute,
http://tusharkute.com
Data Interfaces
• The csv file is a text file in which the values in the columns are
separated by a comma. Let's consider the following data
present in the file named input.csv.
• You can create this file using windows notepad or Ubuntu
gedit by copying and pasting this data. Save the file as
input.csv using the save As All files(*.*) option in
notepad/gedit.
Reading a CSV file
Analysing a CSV file
Analysing a CSV file
Writing into a CSV file
XLSX file
install.packages("xlsx")
Verify installation
Create an xlsx file
Reading xlsx file
• XML is a file format which shares both the file format and
the data on the World Wide Web, intranets, and elsewhere
using standard ASCII text.
• It stands for Extensible Markup Language (XML). Similar to
HTML it contains markup tags. But unlike HTML where the
markup tag describes structure of the page, in xml the
markup tags describe the meaning of the data contained
into the file.
• You can read a xml file in R using the "XML" package. This
package can be installed using following command.
install.packages("XML")
Input data
• Create a XMl file by copying the below data into a text editor.
Save the file with a .xml extension and choosing the file type as
all files(*.*).
<RECORDS>
<EMPLOYEE>
<ID>1</ID>
<NAME>Param</NAME>
<SALARY>623.3</SALARY>
<STARTDATE>1/1/2012</STARTDATE>
<DEPT>IT</DEPT>
</EMPLOYEE> …….
Reading XML file
• The xml file is read by R using the function xmlParse(). It is stored as a list in R.
• Install R Packages
– The following packages are required for processing the URL’s and
links to the files. If they are not available in your R Environment, you
can install them using following commands.
install.packages("RCurl")
install.packages("XML")
install.packages("stringr")
install.packages("plyr")
• Input Data
– We will visit the URL <http://www.geos.ed.ac.uk/~weather/jcmb_ws/>
weather data and download the CSV files using R for the year 2015.
Example:
# Identify only the links which point to the JCMB 2015 files.
filenames <- links[str_detect(links, "JCMB_2015")]
# 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/")
Web data download
Verify file download
install.packages("RMySQL")
Connecting R to MySQL