0% found this document useful (0 votes)
2 views

DAR lecture 8

The document outlines various methods for reading data in R, including CSV files, Excel spreadsheets, and data from web APIs. It also covers reading JSON and XML documents, as well as connecting to databases like MySQL, PostgreSQL, and SQLite using specific R packages. Additionally, it mentions business intelligence tools like JasperDB and Pentaho for reporting and analytics.

Uploaded by

sharmahemant3610
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DAR lecture 8

The document outlines various methods for reading data in R, including CSV files, Excel spreadsheets, and data from web APIs. It also covers reading JSON and XML documents, as well as connecting to databases like MySQL, PostgreSQL, and SQLite using specific R packages. Additionally, it mentions business intelligence tools like JasperDB and Pentaho for reporting and analytics.

Uploaded by

sharmahemant3610
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Methods for reading data in R

Reading data from packages


Reading data from Web/APIs
Reading a JSON document
Reading XML file

Compiled and Presented by


Dr. Chetna Arora
Methods for reading data in R
1. Reading CSV Files
• 1. Reading CSV Files
CSV (Comma Separated Values) files are commonly used to
store tabular data in plain text format.

data <- read.csv("file.csv")


Example: If you have a file named students.csv, you can
read it like this:

students_data <- read.csv("students.csv")


Explanation:
CSV files are simple and widely used for data exchange.
2. Reading Spreadsheet Files (Excel)

To read spreadsheet files like Excel, we can use packages like readxl or
openxlsx.

library(readxl)
data <- read_excel("file.xlsx")

Example: If you have an Excel file named students.xlsx:


students_data <- read_excel("students.xlsx")
Explanation:
Excel files can have multiple sheets, and each sheet can be accessed
by specifying its name or index number.
3. Reading Data from R Packages

Many R packages come with built-in datasets that can be loaded
directly.

data(package = "datasets")
Example: To load the famous iris dataset:

data(iris)
Explanation:
R provides many sample datasets in packages like datasets, which
are useful for learning.
4. Reading Data from Web/APIs
You can use the read.csv function to read data directly from a URL.

data <-
read.csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv")
data<-read.csv("https://raw.githubusercontent.com/jbrownlee/Datasets/
master/iris.csv")
Example:

web_data <- read.csv("https://example.com/data.csv")


Explanation:
Data can be fetched directly from a web URL if it’s in CSV format.
For APIs (APPLICATION PROCESSING INTERFACE), packages like httr or
jsonlite can be used.
Reading JSON Documents

JSON (JavaScript Object Notation) is a popular format for


transmitting structured data.

Command (using jsonlite package):


library(jsonlite)
data <- fromJSON("file.json")

json_data <- fromJSON("data.json")


Explanation:
JSON is widely used for data exchange, especially between web
applications and servers.
The data is structured in key-value pairs.
Reading XML Files

XML (Extensible Markup Language) is a markup
language used to store and transport data.

Command (using xml2 package):


library(xml2)
data <- read_xml("file.xml")
Example:

xml_data <- read_xml("data.xml")


Reading Data from Databases
RODBC

RODBC (R Open Database Connectivity) allows R to interact
with databases like SQL Server, MySQL, etc.

Command (connecting to a database):

library(RODBC)

Explanation:
RODBC helps in connecting to different database systems
(like SQL Server) and running SQL queries.
MySQL and PostgreSQL

To work with these databases, we use the RMySQL or
RPostgreSQL packages.

Command (MySQL example):

library(RMySQL)

Example for PostgreSQL:

library(RPostgreSQL)
SQLite

SQLite databases are self-contained and easy to use with the RSQLite
package.

Command:

library(RSQLite)

Example:

Explanation:
MySQL, PostgreSQL, and SQLite are relational databases. You can run
SQL queries to retrieve data into R.
JasperDB and Pentaho

JasperDB and Pentaho are business intelligence tools that offer
reporting and analytics capabilities, typically connecting to
databases.

JasperDB: A reporting tool often used to extract data from various


databases.
Pentaho: A platform used for data integration, reporting, and
analysis.
Though R doesn’t directly support these tools, they can be connected
using database systems (like MySQL) or through APIs for data
exchange.

You might also like