R tips

How can I execute R code directly from SAS ?
    With this 4 lines of code, it is not necessary to have R in the path : SAS is told where to look for the software.

%let repR=C:\Program Files\R\R-2.6.1\bin;
%let repprog=C:\Users\Franck\Desktop\test;

OPTIONS XWAIT XSYNC;
X " ""&repR\R.exe"" CMD BATCH ""&repprog.\test.R"" ";

How can I see which sql Tables I get on my connection ?

    RODBC is a very useful tool to get data from Excel spreadsheets. But sometimes you don't know the names of the sheet on which the data is stored : yet it is absolutely needed. sqlTables is just what you need !

Let's assume you're really interested in Spanish quarterly national accounts. You get their Excel file, and then you can handle it...

library(RODBC)
ine<-odbcConnectExcel(file.choose())
sqlTables(ine) # -> check which sheet are available
sheet<-sqlFetch(ine,"Feuille1",colnames=FALSE,rownames=FALSE)
close(ine)

How can I data sets ?
    Some functions in the fImport package are very useful.
    ...the SP500 ?
library(fImport)
SP500<-"s=%5EGSPC&b=1&a=00&c=1950&e=25&d=00&f=2008&g=d&x=.csv"
df<- yahooImport(SP500)

sp500<-zoo(df@data[,"Close"],as.Date(as.vector(df@data[,1])))

plot(sp500)

    ...the CAC40 index ?
This is how to get and plot the weekly time series of the CAC40 index, from Yahoo (the time series starts in march, 1990) :
library(fImport)
wCAC40<-"s=%5EFCHI&b=1&a=00&c=1980&e=25&d=00&f=2008&g=w&x=.csv"
df<- yahooImport(wCAC40)

res<-apply(as.matrix(df@data[,-1]),2,as.numeric)
res<-zoo(res,order.by=as.Date(df@data[,1]))

plot(res[,"Close"])


This is the same for the monthly time series :
library(fImport)
mCAC40<-"s=%5EFCHI&b=1&a=00&c=1980&e=25&d=00&f=2008&g=m&x=.csv"
df<- yahooImport(mCAC40)

res<-apply(as.matrix(df@data[,-1]),2,as.numeric)
res<-ts(res,start=c(1990,3),freq=12)

plot(res[,"Close"])

How can I plot colors in a matrix ?
    Something like this works just fine :
essai<-matrix(rnorm(20*26),20,26)
library(RColorBrewer) # nice colors, I dig this package !
couleurs<-brewer.pal(9,"Blues")
col<-matrix(couleurs[cut(essai
,breaks=9,labels=FALSE)],20,26)
col[3,25]<-"red" # test


mat<-essai
n<-nrow(mat)
m<-ncol(mat)
xlabels<-paste("L",1:26,sep="")
ylabels<-paste("C",1:20,sep="")
cex.axis<-0.8

heights<-rep(1,n)
widths<-rep(1,m)


## Code starts here
abs<-c(0,cumsum(widths))
ord<-rev(c(0,cumsum(heights)))

par(mar=c(2,2,3,2),las=1,mgp=c
(1,0,0),tcl=-0.2)
plot(x=range(abs),y=range(ord)
,type="n",main="Test",xaxs="i",yaxs="i",xaxt="n",yaxt="n",xlab="",ylab="",line=1.5,cex.main=2)
rect(    xleft=rep(abs[-(m+1)],each=n),
    xright=rep(abs[-1],each=n),
    ybottom=rep(ord[-1],m),
    ytop=rep(ord[-(n+1)],m),
    col=col,
    border=grey(0.5)
)
box()
if (!is.null(xlabels)) {
    axis(side=1,at=(abs[-1]+abs[-
(m+1)])/2,labels=xlabels,tick=FALSE,cex.axis=cex.axis,padj=0)
    axis(side=3,at=(abs[-1]+abs[-
(m+1)])/2,labels=xlabels,tick=FALSE,cex.axis=cex.axis,padj=0)
}
if (!is.null(ylabels)) {
    axis(side=2,at=(ord[-1]+ord[-
(n+1)])/2,labels=ylabels,tick=FALSE,cex.axis=cex.axis)
    axis(side=4,at=(ord[-1]+ord[-
(n+1)])/2,labels=ylabels,tick=FALSE,cex.axis=cex.axis)
}

How does regular expressions work ?
    I don't know, but here are some examples
Names ending in .Rdata "\\.RData$"
Replace multiples blanks by one blank : gsub("[ ]+"," ","first   next")
Remove starting blanks : gsub("^[ ]*","","   aaa bb")