<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/connecting-r-to-an-oracle-database/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://geraldbelton.com/connecting-r-to-an-oracle-database/"> R – Gerald Belton</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R is a very popular language for doing analytics, and particularly statistics, on your data. There are a number of R functions for reading in data, but most of them take a delimited text file (such as .CSV) for input. That’s great if your existing data is in a spreadsheet, but if you have large amounts of data, it’s probably stored in a<a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Relational_database"> relational database</a>. If you work for a large company, chances are that it is an Oracle database.</p>
<p>The most efficient way to access an Oracle database from R is using the RODBC package, available from CRAN. If the RODBC package is not installed in your R environment, use the <i>install.packages(“RODBC”)</i> command to install it. ODBC stands for Open DataBase Connectivity, an open standard application programming interface (API) for databases. ODBC was created by the SQL Access Group and first released in September, 1992. Although Microsoft Windows was the first to provide an ODBC product, versions now exist for Linux and Macintosh platforms as well. ODBC is built-in to current versions of Windows. If you are using a different operating system, you’ll need to install on OBDC driver manager.</p>
<p>Before you can access a database from R, you’ll need to create a Data Source Name, or DSN. This is an alias to the database, which provides the connection details. In Windows, you create the DSN using the ODBC Source Administrator. This tool can be found in the Control Panel. In Windows 10, it’s under System and Security -> Administrative Tools -> ODBC Data Sources. Or you can just type “ODBC” in the search box. On my system, it looks like this:</p>
<p><img sizes="(max-width: 810px) 100vw, 810px" alt="" class="wp-image-217" src="https://i2.wp.com/geraldbelton.com/wp-content/uploads/2017/05/ODBC-1.png?resize=810%2C544" data-recalc-dims="1"></p>
<p>As you can see, I already have a connection to an Oracle database. To set one up, click Add, and you’ll get this box:</p>
<p><img sizes="(max-width: 517px) 100vw, 517px" alt="" class="wp-image-218" src="https://i0.wp.com/geraldbelton.com/wp-content/uploads/2017/05/ODBC2.png?resize=517%2C391" data-recalc-dims="1"></p>
<p>Select the appropriate driver (in my case, Oracle in OraDB12Home1) and click the Finish button. A Driver Configuration box opens:</p>
<p><img sizes="(max-width: 530px) 100vw, 530px" alt="" class="wp-image-219" src="https://i2.wp.com/geraldbelton.com/wp-content/uploads/2017/05/ODBC3.png?resize=530%2C399" data-recalc-dims="1"></p>
<p>For “Data Source Name,” you can put in almost anything you want. This is the name you will use in R when you connect to the database.</p>
<p>The “Description” field is optional, and again, you can put in whatever you want.</p>
<p>TNS Service Name is the name that you (or your company data base administrator) assigned when configuring the Oracle database. And “User ID” is your ID that you use with the database.</p>
<p>After you fill in these fields, click the “Test Connection” button. Another box pops up, with the TNS Service Name and User ID already populated, and an empty field for your password. Enter your password and click “OK.” You should see a “Connection Successful” message. If not, check the Service Name, User ID, and Password.</p>
<p>Now you are ready to connect R to the database.</p>
<p>Here’s the R code that you need:</p>
<pre><em># Load RODBC package</em>
library(RODBC)

<em># Create a connection to the database called &quot;channel&quot;</em>
channel &lt;- odbcConnect(&quot;DATABASE&quot;, uid=&quot;USERNAME&quot;, pwd=&quot;PASSWORD&quot;)

<em># Query the database and put the results into the data frame</em>
<em># &quot;dataframe&quot;</em>

 dataframe &lt;- sqlQuery(channel, &quot;
 SELECT *
 FROM
 SCHEMA.DATATABLE&quot;)

# When finished, it's a good idea to close the connection
odbcClose(channel)</pre>
<p>A couple of comments about this code are in order:</p>
<p>First, I don’t like the idea of having a password appear, unencrypted, in the R program. One possible solution is to prompt the user for the password before creating the connection:</p>
<pre>pswd &lt;- readline(&quot;Input Password: &quot;)
channel &lt;- odbcConnect(&quot;DATABASE&quot;, uid=&quot;USERNAME&quot;,  pwd=pswd)</pre>
<p>This will enable the connection to be made without compromising the security of the password.</p>
<p>Second, the sqlQuery will pass to Oracle whatever is inside the quotation marks. This is the workhorse function of the RODBC package. The term ‘query’ includes any valid SQL statement including table creation, updates, etc, as well as ‘SELECT’s.</p>
<p>Finally, I should mention that R works with data that is loaded into the computer’s memory. If you try to load a really huge database into memory all at once, it will a) take a very long time, and b) possibly fail due to exceeding your computer’s memory capacity. Of course, relational database systems like Oracle are the natural habitat of very large data sets, so that may be your motivation for connecting R to Oracle in the first place. Carefully constructed SQL Queries will let Oracle do the work of managing the data, and return just the data that R needs for performing analytics.</p>
<p>Writing SQL Queries is beyond the scope of this blog post. If you need help with that, there are plenty of free tutorials on the web, or you might find this book helpful: <a target="_blank" rel="nofollow" href="http://amzn.to/2qGuFYa">Oracle 12c for Dummies</a></p>
<p><a target="_blank" rel="nofollow" href="https://www.amazon.com/Oracle-12c-Dummies-Chris-Ruel/dp/1118745310/ref=as_li_ss_il?ie=UTF8&qid=1495311290&sr=8-12&keywords=oracle+database&linkCode=li2&tag=gerald09-20&linkId=4d4a8cc765a27634d6aaedd20e8c74b1"><img src="http://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=1118745310&Format=_SL160_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=gerald09-20" border="0"></a><img height="1" alt="" width="1" src="https://ir-na.amazon-adsystem.com/e/ir?t=gerald09-20&l=li2&o=1&a=1118745310" border="0"></p>
<p> </p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://geraldbelton.com/connecting-r-to-an-oracle-database/"> R – Gerald Belton</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/f1TEED23_yU"></a></div>