- When install R, install both 32-bit and 64-bit versions.
- Install Jave Runtime Environment (JRE), both 32-bit and 64-bit.
Download both versions and install them respectively, using their default installation folders, so
- 32-bit JRE automatically goes to folder C:\Program Files (x86)\Java\jre7
- 64-bit JRE goes to folder C:\Program Files\Java\jre7.
- Set up two versions of JREs in Eclipse
- Set up two R environments in Eclipse
Menu: Run --> Preferences --> StarET --> Run/Debug --> R Environments , assigning different names for different R environments (note: the R_HOME location are same for both environments)
The most important setting here is that the 'Architecture' fields for the two environments have to be set as x86_64 and x86, respectively.
- Create two R consoles in Eclipse<>
Menu: Run --> Run Configurations --> R Console, right click 'New' to create two R consoles, as shown below
Make sure to set correct versions of the R environments and JRE in the corresponding R consoles
- To activate one R console, Run --> Run Configurations --> R Console --> Choose one console and click 'Run' button....that's it !!!
Top Menu
▼
Pages
▼
Thursday, July 18, 2013
Configure Eclipse IDE to work for both 32-bit and 64-bit R
Sometime we need to make Eclipse compatible simultaneously for both 32-bit and 64-bit versions of R. For example, we may have the 64-bit R as the default configuration. However, if we want to run the odbcConnectExcel function which only works under 32-bit scenary, we have to switch the current active configuration to 32-bit R. This tutorial tells how to make two configurations of 32-bit and 64-bit R coexist in one Eclipse IDE.
Monday, July 15, 2013
Use Eclipse as IDE for R under Windows
Background
Eclipse is an open source Integrated Development Environment (IDE). As with Microsoft's Visual Studio product, Eclipse is programming language-agnostic and supports any language having a suitable plugin for the IDE platform. For Eclipse, the R language plugin is StatET.
The StatET plugin enables Eclipse to connect to R. You may have been using R console or a simple editor like notepad or wordpad to write and submit R programs to R before. With Eclipse and the StatET plugin for R, you will use the rich editing environment provided by Eclipse and the StatET plugin to write and submit those programs to R from within the Eclipse IDE (Integrated Development Environment). The result will be a better, less error-prone development environment for your R software development.
Above figure shows Eclipse, StatET with R, and the R debugger (bottom window) at work. The R debugger is an R package library and has its own graphical output window separate from Eclipse.
Install Eclipse
Configure the StatET Eclipse plugin
Testing the Eclipse/StatET/R IDE Configuration
Eclipse is an open source Integrated Development Environment (IDE). As with Microsoft's Visual Studio product, Eclipse is programming language-agnostic and supports any language having a suitable plugin for the IDE platform. For Eclipse, the R language plugin is StatET.
The StatET plugin enables Eclipse to connect to R. You may have been using R console or a simple editor like notepad or wordpad to write and submit R programs to R before. With Eclipse and the StatET plugin for R, you will use the rich editing environment provided by Eclipse and the StatET plugin to write and submit those programs to R from within the Eclipse IDE (Integrated Development Environment). The result will be a better, less error-prone development environment for your R software development.
Above figure shows Eclipse, StatET with R, and the R debugger (bottom window) at work. The R debugger is an R package library and has its own graphical output window separate from Eclipse.
Install Eclipse
- Download the latest stable Eclipse release (I use Eclipse Classic 4.2.1, 183 MB, as of 20-Oct-2012). For 64-bit Windows be sure to download and install the 64-bit version of Eclipse AND install the 64-bit JRE (Java Runtime Environment). Once downloaded and extracted, copy folders and files under the obtained Eclipse folder (for example, eclipse-SDK-4.2.1-win32-x86_64) to your desired installation location, such as C:\eclipse. Add a desktop shortcut to the Eclipse executable (c:\eclipse\eclipse.exe) if you like.
- Your system may already have Java installed, in which case you can skip these Java installation steps. 64-bit Java runtime environments (JREs) are designated explicitly on the java.com website as 64-bit, for example 'Windows 7/XP/Vista/2003/2008 (64-bit)' or similar. If you do not see the 64-bit JRE offering for Windows then you are likely running 32-bit Windows and should use the 32-bit JRE and Eclipse.
- If you are running a 64-bit Windows and you still do not see the 64-bit offerings on java.com then switch to another browser (64-bit).
- From the Eclipse menu bar click Help -> Install New Software
- Click the 'Add' button. The "Add Site" dialog appears
- Type in a friendly name for your remote resource, such as StatET
- Paste the URL (http://download.walware.de/eclipse-3.8, this is the latest version available at StatET website, as of 20-Oct-2012) into the 'Location' box, make sure to check the option of Contact all update sites during install to find required software, then click 'OK'
- Select (check) the package components that you want to install, then click 'Next'
- A review screen showing your selection(s) displays. Click 'Next'
- Accept the license agreement, then click 'Finish'
- Now Eclipse will install StatET, but it will take a few minutes. During the installation you may be asked to accept a digital certificate from Eclipse.org.
Configure the StatET Eclipse plugin
- Install R, if you need to. NOTE: as far as Eclipse is concerned, it makes no difference if you run 32 or 64-bit R. Just be sure that you identify 32 or 64-bit R to Eclipse, which we do in the next steps (among other things).
- From the Eclipse menu bar click Help -> Cheat Sheets: StatET: R in Eclipse to put up the official help. This is a handy, built-in guideline, but some of the steps inaccurately describe the menu choices (and one reason why I wrote these instructions). It is a good reference, though, for your later work, so keep it in mind. Click the 'Cancel' button to close the Cheat Sheet Selection dialog.
- From the Eclipse menu bar click Window -> Preferences: StatET : Run/Debug: R Interaction: Select 'New Console Inside Eclipse' for the 'Connector used to run R code'. Note that this is the default so it might already be this way. This is how I run mine. Be sure to click 'Apply' if you change the setting.
- Back in the left pane click 'R Environments' (StatET : Run/Debug: R Environments, which is directly above the 'R Interaction' of the previous step). The 'R Environments' pane now appears on the right.
- Click the pane's 'Add' button. The 'Add R Environment Configuration' dialog appears.
- Type in a friendly name in the 'Name' box, such as R.x64 2.15.1 or whatever your R version is, for example.
- For the 'Location (R_HOME)' box, paste in the path to your R install directory, for example C:\Program Files\R\R-2.15.1. Make sure the 'Architecture' field is set to your R type (32 or 64-bit), such as x86_64.
- Click 'OK' to save your changes and close the dialog, which returns us to the 'R Environments' pane, which now shows our newly created R environment.
- Click 'Apply' and 'OK' to save and close the 'R Environments' pane.
- From the Eclipse menu bar click Run -> Run Configurations. The 'Run Configurations' dialog box appears.
- In the left pane click 'R Console', right click and select 'New' to define a new console entry with a friendly name in the 'Name' box, such as 'MyConsole'.
- Under the 'Main' tab ensure that 'Launch Type' is set properly for your configuration. If you are uncertain, choose 'Rterm' (this selection is VERY IMPORTANT).
- For 'Working Directory' just provide a path to your projects directory, for example C:\d2\SOFTWARE_DEVELOPMENT\Research\R\Projects
- No further changes are required, but you can check to make sure that the 'R Environment' under the next tab ('R Config') is set to that environment you named previously.
- Click 'Apply' and then 'Close' to close the dialog
- Set perspective for R project If you can not see the R-project under File --> New, switch Eclipse's perspective to StatET by clicking Window --> Open Perspective --> Other --> StatET.
Testing the Eclipse/StatET/R IDE Configuration
- From the Eclipse menu bar click File -> New -> R-Project.
A 'New R-Project' dialog box appears, so give the new project a name like 'test'. - From the Eclipse menu bar, click File -> New -> R-Script File. Name your new script and click 'Finish'.
A new script window appears in the Eclipse IDE. - Paste in or type some R test code, for example:
# TODO: Add comment # # Author: mark qu ################################################################ # some test code... a = 2 b = 3 c = a+b c - To run the script, click inside the R script window to give it focus. Then, from the Eclipse menu bar, click Run -> Run : R Script in R Submitting Directly -OR- R Script in R via Source. You can then execute by simply clicking Run -> Run. You can also select the Run icon (green ball with the white triangle inside) from the Eclipse toolbar.
Sunday, July 14, 2013
Import date from Excel spreadsheet into R using RODBC package
Package RODBC can be used not only for ODBC database connectivity, but also for importing Excel spreadsheet data into R, as shown in following R code:
Note:
library(RODBC)
data=odbcConnectExcel(file.choose())
mydata=sqlFetch(data,"Sheet1")
odbcClose(data)
mydata
Here, the odbcConnectExcel establishes a connection to an external Excel spreadsheet.Note:
- Function 'odbcConnectExcel' is only usable with 32-bit R. Under Eclipse IDE under 64-bit Windows, you need to activate 32-bit R console in order to run above code.
- It is said 'odbcConnectExcel2007' works for 64-bit environment, but I had no luck to make it works.
Import data from Microsoft SQL server to R using RODBC package
As preparation, first create an ODBC driver connection for SQL Server by
Control Panel >> Administrative Tools >> Data Sources (ODBC)
Then import data from SQL server with following R code
Control Panel >> Administrative Tools >> Data Sources (ODBC)
Then import data from SQL server with following R code
library(RODBC)
conn <- odbcConnect(dsn="ACCPAC 54 SQL",uid="my_ID",pwd="my_PWD")
#sqlTables(channel)
queryResult <- sqlQuery(conn, "SELECT * FROM TableName")
odbcClose(conn)
dim(queryResult)
Alternatively, without creating ODBC driver connection, you can use following R code to directly set up connection
library(RODBC)
conn <- odbcDriverConnect("Driver=SQL Server; Server=xxx.xx.xxx.xx; Database=test_DB; Uid=my_ID; Pwd=my_PWD;")
#sqlTables(channel)
queryResult <- sqlQuery(conn, "SELECT * FROM TableName")
odbcClose(conn)
dim(queryResult)
Import data from Microsoft SQL server to R using RJDBC package
Environment
- Windows 7, 64-bit
- R 2.15.1 (32- & 64-bit)
- Microsoft SQL Server 2008
- Download JDBC driver from Microsoft ( Link ) and install it so that RJDBC plugin is able to interact with SQL Server database via the Java interface.
- You can install the driver to a local folder, such as C:\Program Files\Microsoft SQL Server JDBC Driver 3.0
- The latest RJDBC release is available from CRAN, install it using following command:
install.packages("RJDBC",dep=TRUE) - Usage of JDBC
drv <-JDBC (driverClass, classPath) conn <- dbConnect(drv, "jdbc:sqlserver://serverName", "userID", "password") - driverClass --- name of the Java class of the JDBC driver to load. If empty, it is assumed that corresponding JDBC drivers were loaded by other means.
For Microsoft SQL Server, driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver" - classPath --- class path that needs to be appended in order to load the desired JDBC driver. Usually it is the path to the JAR file containing the driver. For example, "C:/Program Files/Microsoft SQL Server JDBC Driver 3.0/sqljdbc_3.0/enu/sqljdbc4.jar"
- When specifying database server name, I tried following two ways, and looks like both worked
- jdbc:sqlserver://xxx.xx.xxx.xx
- jdbc:sqlserver://xxx.xx.xxx.xx:port;database=databaseName
- Example code:
library(RJDBC) drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "C:/Program Files/Microsoft SQL Server JDBC Driver 3.0/sqljdbc_3.0/enu/sqljdbc4.jar") conn <- dbConnect(drv, "jdbc:sqlserver://xxx.xx.xxx.xx:1433;database=My_MARKET_DATA", "admin", "123456789") #dbListTables(conn) sqlText <- paste("SELECT * FROM myTable", sep="") queryResults <- dbGetQuery(conn, sqlText)








