Important Notice !!!

This blog has been discarded and no update any more.

Please visit http://www.quantlego.com for active updates.

Sorry for any inconvenience.





Showing posts with label Note+R. Show all posts
Showing posts with label Note+R. Show all posts

Thursday, July 18, 2013

Configure Eclipse IDE to work for both 32-bit and 64-bit R

| |
0 comments
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.
  1. When install R, install both 32-bit and 64-bit versions.

  2. 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.
  3. Set up two versions of JREs in Eclipse

  4. 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.

  5. 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



  6. To activate one R console, Run --> Run Configurations --> R Console --> Choose one console and click 'Run' button....that's it !!!
Read More

Sunday, July 14, 2013

Import date from Excel spreadsheet into R using RODBC package

| |
0 comments
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:
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.
Read More

Import data from Microsoft SQL server to R using RODBC package

| |
0 comments
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
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)
Read More

Import data from Microsoft SQL server to R using RJDBC package

| |
0 comments
Environment
  • Windows 7, 64-bit
  • R 2.15.1 (32- & 64-bit)
  • Microsoft SQL Server 2008
Installation & Usage
  1. 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
  2. The latest RJDBC release is available from CRAN, install it using following command:
    install.packages("RJDBC",dep=TRUE)
  3. 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
  4. 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)
Read More

Monday, June 17, 2013

A beginner's tutorial for R

| |
0 comments
  • Some common commands
  • # Quit R
    q()
    q(save="no")
     
    # Setting the default working directory
    setwd(file="c:\\AnyDirectory\\")
    setwd(file="c:/AnyDirectory/")
     
    # Get the default working directory
    getwd()
     
    # See which variables are currently in the workspace
    ls()
     
    # Remove a list of variables
    rm(var1,var2,var3,...)
     
    # Remove all objects from workspace
    rm(list=ls())
     
    # Note: the variables whose names begin with a dot are not listed by ls(), so they are not removed by above command. 
    # Instead, you can use following command to remove them
    rm(list=ls(all=TRUE))
     
    # Load a package
    library(PackageName)
     
    # Sink R output to a file
    sink(FileName)
    sink(NULL) # stop the sink

  • You can use either single- or double-quote for text
  • > cat("I said:\"Hello!\"",'then smile.')
    I said:"Hello!" then smile.
    > cat("This is Eric\'s book,",'and that is Mike\'s toy.')
    This is Eric's book, and that is Mike's toy.
Read More
Powered by Blogger.