If the data, that you want to perform analyze is stored in a SQL (Structured Query Language) database such as MySql, Oracle, or Microsoft SQL then the RODBC R package can be used to import the data into R. ODBC (Open Database Connectivity) is a standard protocol used to connect to databases regardless of the operating system they are running.

In order to connect to the database you will need the username, password, the RODBC library and the database name.

The RODBC library can be installed and loaded into R using the following R code


Now, we will connect to the database, with the database name CARS:

mydb<-odbcConnect("CARS",uid="username",pwd="password") -- connect to database

After an open database connection has been successfully established, the sqlQuery() function can be used to create a data.frame from the database rows.

data_query <- "SELECT * from car_names WHERE price > 15000"; #create the sql query to use in sqlQuery function

car_data <- sqlQuery(channel=mydb, query=data_query, stringAsFactors=False); # stores the data in a data.frame containing all the rows
#from data_query. stringAsFactor = False is used so R does not transform the character data into factors

After you are done the connection can be closed by.