
Cross Platform

dbConnect() function
This function is used to connect DBMS so that data can be queried; see the final example below.
3 dbWriteTable() function
This function writes the table in R into a database for querying.
Parameter meanings:
- conn: the connection made
- name: the name of the table to be made
- value: the data to be brought in as a database
dbListTables() function
Shows a list of tables in a database and a list of variables in a table.
dbGetQuery() function
Performs the query given on the table selected.
Parameter meanings:
- conn: the connection to the database
- statement: the SQL statement to be performed
dbDisconnect() function
Disconnect the connection
Example:
data(airquality)
library(RSQLite)
# Connection to the database
connection <- dbConnect(SQLite(),'airData.db')
# Write the table to the database
dbWriteTable(connection, "airData", airquality, overwrite = TRUE)
# List the tables in a database
dbListTables(connection)
## [1] "airData"
# List the variables in a table
dbListTables(connection, "airData")
## [1] "airData"
# Run the query on the table
dbGetQuery(connection, "SELECT * FROM airData WHERE Wind > 10")
## Ozone Solar.R Wind Temp Month Day
## 1 12 149 12.6 74 5 3
## 2 18 313 11.5 62 5 4
## 3 NA NA 14.3 56 5 5
## 4 28 NA 14.9 66 5 6
## 5 19 99 13.8 59 5 8
## 6 8 19 20.1 61 5 9
## 7 14 274 10.9 68 5 14
## 8 18 65 13.2 58 5 15
## 9 14 334 11.5 64 5 16
## 10 34 307 12.0 66 5 17
# Disconnect connection
dbDisconnect(connection)
Note: for brevity, only the first 10 lines of the query were shown.
Using Java in R
Java is a programming language similar to C, C++, and Python. In R studio, we can embed Java code using rJava. There are many functions inside this package:
.jinit()
: Initializes the java virtual machine.jaddClassPath()
: Adds the given path to the R class path.jClassPath()
: Used to see what is in the class path.jnew()
: Create a new java object.jcall()
: Call a java method with the given arguments
Parameter meanings:
- class: the file name
- obj: the java object
- returnSig: the return type of the method
- method: name of the method from the java object
Example:
library(rJava)
.jinit()
## [1] 0
.jaddClassPath("C:\\Users\\username\\Google Drive\\Java\\bin")
.jclassPath()
## [1] "C:\\Users\\username\\Documents\\R\\win-library\\3.4\\rJava\\java"
## [2] "C:\\Users\\username\\Google Drive\\Java\\bin"
addNums <- .jnew("AddNums")
addTest <- .jcall(addNums, "I", "added")
addTest
## [1] 6
Using C++ in R
One of the first programming languages invented was C. Unfortunately, it did not have any classes, which made it harder to use. C++ was then created from C, but with classes. In fact, R was written in C++, so you are actually running a C++ program every time you use R.
A benefit of using C++ code is that it compliles quickly, which makes the computer run faster, however, it is also more complex, making it difficult to write.
To use C++ within R, the Rcpp package is available. C++ code is encased in between apostrophes and parenthesis.
Example:
library(Rcpp)
cppFunction('int addThreeNums(int num, int num2, int num3) {
int sum = num + num2 + num3;
return sum;
}')
addThreeNums
## function (num, num2, num3)
## .Primitive(".Call")(<pointer: 0x0000000071281600>, num, num2,
## num3)
addThreeNums(1, 2, 3)
## [1] 6
USing Python in R
Python is an interpreted programming language. Like Java, C and C++, it has its advantages and disadvantages. The code to complete a task using the Python language is not complex and it is easy to learn. However, it Lacks a simple but powerful input reading function/class.
Using the rPython package, we can embed python code in R studio. There are several functions inside the rPython package.
python.assign(variable name, variable)
python.exec( function/ formula)
python.get(variable name)
python.load(file)
python.call(function)
Note: all python code are written inside the quotation marks.
library(rPython)
a <- 1:4
python.assign( "list", a )
python.exec( "length = len( list)" )
python.get( "length" )
we create a list from 1 to 4. Then we assign the list 1 to 4 to "list" in python, and use the python.exec()
to run the length function in python and use python.get()
to get the length of the list.
python.exec( c( "def addThreeNums(num1, num2, num3):
return num1 + num2 + num3") )
python.call( "addThreeNums", 1, 2, 3)
To execute the python code, we can just copy and paste the python code into python.exec()
and call the python function python.call()
to call the function in python. If we have a python file, we can simply load it.
python.load("addThreeNumsPython.py")
python.call("addThreeNums", 1, 2, 7)
Note: The rPython package only works on a MAC.