Connecting MySQL with R Programming Language
R requires RMySQL package to create a connection object which takes username, password, hostname and database name while calling the function. dbConnect() function is used to create the connection object in R.
Syntax: dbConnect(drv, user, password, dbname, host)
Parameter values:
- drv represents Database Driver
- user represents username
- password represents password value assigned to Database server
- dbname represents name of the database
- host represents host name
Example:
R
# Install package install.packages ( "RMySQL" ) # Loading library library ( "RMySQL" ) # Create connection mysqlconn = dbConnect ( MySQL (), user = 'root' , password = 'welcome' , dbname = 'GFG' , host = 'localhost' ) # Show tables in database dbListTables (mysqlconn) |
Tables present in the given database:
Output:
Loading required package: DBI [1] "articles"
Working with Databases in R Programming
Prerequisite: Database Connectivity with R Programming
In R programming Language, a number of datasets are passed to the functions to visualize them using statistical computing. So, rather than creating datasets again and again in the console, we can pass those normalized datasets from relational databases.