R Statistical Programming Using MariaDB as the Background Database
- Introduction to R
- The R environment
- Using R with MariaDB
- R Installation
- Data Transfer between R and MariaDB
- R Programming Resources
Introduction to R
R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …), graphical techniques, machine learning packages and is highly extensible.
One of R’s strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.
The R environment
R is an integrated suite of software facilities for data manipulation, calculation, and graphical display. It includes:
• an effective data handling and storage facility,
• a suite of operators for calculations on arrays, in particular matrices,
• a large, coherent, integrated collection of intermediate tools for data analysis,
• graphical facilities for data analysis and display either on-screen or on hardcopy, and
• a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.
Using R with MariaDB
Some basic notions / tips on how to use R along with MariaDB are the following:
A. The recommended R distribution is “Microsoft R Open”: https://mran.microsoft.com/open/
B. The recommended R GUIs are RStudio Desktop, or RStudio Server: https://www.rstudio.com/products/rstudio/
An alternative would be Microsoft Visual Studio 2015 or 2017: https://www.visualstudio.com/vs/rtvs/
“Microsoft R Open” and “MariaDB Server” can be installed either in the same server, or in different servers, as an ODBC communication protocol will be used for the exchange of data between the two environments.
Data Transfer between R and MariaDB
For the transfer of data between MariaDB Server and R Environment, it is recommended R's "odbc" Package:
- “odbc" is a new R package available on CRAN (Since 2017-02-05), and maintained by RStudio, which is designed to comply with the DBI specification.
- Tutorials on how to use R's "odbc" package can be found here:
- Setting up ODBC Drivers: https://db.rstudio.com/drivers/
- "odbc" R Package: https://db.rstudio.com/odbc/#usage
The "odbc" package requires to have previously installed the MariaDB or MySQL ODBC connector:
For installing the "odbc" package from CRAN, execute in R:
“RMariaDB” R library, is a modern 'MariaDB' client based on 'Rcpp'.
For installing RMariaDB package through CRAN, execute the following R statement:
Other Packages: "readr", "RODBC"
There are other alternatives for data transfer between R and MariaDB:
- “readr” R package, for writing / reading CSV files. To be used in MariaDB along with “LOAD DATA INFILE”.
- "RODBC" R package: Robust and well-tested (Since 2000-05-24) package which enables data transfer between R and MariaDB by means of an ODBC connector: https://cran.r-project.org/web/packages/RODBC/index.html
- It is slightly slower than RStudio's new "odbc" package (See benchmarks): https://db.rstudio.com/odbc/
- For bug report to the RODBC package maintainer, use the following R statement: bug.report(package = "RODBC")
- A vignette on how to use the RODBC package can be found here: https://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf
R Programming Resources
Three excellent and recommended books for learning how to program in R are the following:
- R Cookbook (O’Reilly Media; Paul Teetor): http://shop.oreilly.com/product/9780596809164.do
- R Graphics Cookbook (O’Reilly Media; Winston Chang): http://shop.oreilly.com/product/0636920023135.do
- R for Data Science (O’Reilly Media; Garrett Grolemund, Hadley Wickham): http://shop.oreilly.com/product/0636920034407.do
An extract of the books can be found here:
An excellent book for understanding the underlying statistics in the R packages is:
- Practical Statistics for Data Scientists (O’Reilly Media; Peter Bruce, Andrew Bruce): http://shop.oreilly.com/product/0636920048992.do
C) Statistical / Unsupervised Machine Learning, Deep Learning and Artificial Intelligence
The R Programming language has support for the H2O.ai library (h2o), which enables in-memory multi-cluster machine learning models.
For installing H2O.ai through CRAN, execute:
- H2O.ai Webpage: https://www.h2o.ai/
- Practical Machine Learning with H2O (O'Reilly Media; Darren Cook): http://shop.oreilly.com/product/0636920053170.do
The following R Statements can be used for importing a MariaDB table to H2O.ai using the R Front End:
- import_sql_table: "This function imports a SQL table to H2OFrame in memory. This function assumes that the SQL table is not being updated and is stable. Users can run multiple SELECT SQL queries concurrently for parallel ingestion".
- import_sql_select: "This function imports the SQL table that is the result of the specified SQL query to H2OFrame in memory. It creates a temporary SQL table from the specified sql_query. Users can run multiple SELECT SQL queries on the temporary table concurrently for parallel ingestion, and then drop the table".
connection_url <- "jdbc:mariadb://172.16.2.178:3306/ingestSQL?&useSSL=false" username <- "root" password <- "abc123" # Whole Table: table <- "citibike20k" my_citibike_data <- h2o.import_sql_table(connection_url, table, username, password) # SELECT Query: select_query <- "SELECT bikeid from citibike20k" my_citibike_data <- h2o.import_sql_select(connection_url, select_query, username, password)
NOTE: Be sure to start the h2o.jar in the terminal with your downloaded JDBC driver in the classpath:
java -cp <path_to_h2o_jar>:<path_to_jdbc_driver_jar> water.H2OApp
D) Cheatsheets: Concept Summary
- Rstudio Cheatsheets are a recommended and valuable resource: https://www.rstudio.com/resources/cheatsheets/
- Along with the following Base R reference card: https://cran.r-project.org/doc/contrib/Short-refcard.pdf
E) Search Engine
- For searching any R related information, the following web searcher is recommended (Based on Google): http://rseek.org/