Posts

Showing posts with the label Oracle

Connecting R to An SQL Database (PostgreSQL, MySQL, Oracle)

While reading data into R isn't difficult, there are a variety of reasons why you would want R to directly pull data from a database. Whatever your situation, I will take you through the process of connecting R to that DB and querying data from it. My favorite SQL platform is PostgreSQL because of its simplicity and versatility. In other words, I use R for most things and PostgreSQL works well with R. You can obtain it here , if needed. I suggest that you download  RStudio if you do not already have it as it makes installing packages and other essential tasks easier. The following code is essentially the same for all SQL platforms, the difference being which R package you load. For postgreSQL we use, library( RPostgreSQL ) # for a local instance of postgres con<- dbConnect(dbDriver( "PostgreSQL" ) , dbname = "myDB" ,host = "localhost" ,port = "5432" ) # or to connect to a server it will look something like... con<- dbConnec