Need help connecting R to Oracle? You’ve come to the right place!


Congrats! Using R is great.
Follow these steps below and you’ll be working with CCI data in clean data frames in no time. If you have any questions, feel free to reach out to zalexander@nycourts.gov. I’d be happy to help at any point.

options(java.parameters = "-Xmx8g")
memory.limit(size=10000000000024)
## [1] 1e+13
require(RJDBC)

After running this chunk, if you get the following output, you’ll have to do some extra work:

Fixing rJava

To fix this, please do the following:


Connect to JCA SQL Database

I put the file on my desktop, in a folder called “Oraclejar”, so the file path for me is: + C:/Users/zalexander/Desktop/Oraclejar/ojdbc6.jar

jdbcDriver =JDBC("oracle.jdbc.OracleDriver",classPath="C:/Users/zalexander/Desktop/Oraclejar/ojdbc6.jar")

Connect to DB

# jdbcConnection =dbConnect(jdbcDriver, "jdbc:oracle:thin:@//10.1.16.75:1521/PROD2")

You should now be connected to CCI’s database! The cool thing about the RJDBC package is that it has custom query functions built in. To read in a .sql query file, you can use this one below as an example. You can change/adjust your sql script to pull in the variables you’d like, just make sure the file is pointing to the right place. Once your query is saved as a variable, you can fun the final chunk.


Import SQL Query

# my_query <- 'R:/Research_Demonstration Projects/Felony ATI downtown/Data_Pulls_SQL/R_db_pull_sql.sql'
# my_query <- paste(readLines(my_query), collapse='\n')

Save as Dataframe

# df <- dbGetQuery(jdbcConnection, my_query)

You can now use this dataframe to do whatever you want!