Translate

Friday, 20 May 2016

send emails from R through Outlook


This assumes Outlook Application is installed and your account is set up etc...

Also, you may need to restart Outlook after installing the package in R, if you get an error like 'Error: Exception occurred.'.


library(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")  
outMail = OutApp$CreateItem(0) 

outMail[["To"]] = "recipient's email address" 
outMail[["subject"]] = "subject" 
outMail[["body"]] = "body text" 

outMail$Send()



To send emails to multiple recipients, use semicolon (;) to separate email addresses:

OutApp <- COMCreate("Outlook.Application") 
outMail = OutApp$CreateItem(0)

outMail[["To"]] = "recipient's email address 1; recipient's email address 2"
outMail[["subject"]] = "subject" 
outMail[["body"]] = "body text" 

outMail$Send()



To send emails with attachment(s):

OutApp <- COMCreate("Outlook.Application") 
outMail = OutApp$CreateItem(0)

outMail[["To"]] = "recipient's email address"
outMail[["subject"]] = "subject" 
outMail[["body"]] = "body text" 

outMail[["Attachments"]]$Add("full path to file")     
#e.g. "C:/Users/Documents/someFile.txt"
#note the use of forward slash instead of back slash as you'd normally do in R when setting path to the attachment 

outMail$Send()



To embed table within the body of the email:
library(pander) 

panderOptions('table.split.table', Inf)

OutApp <- COMCreate("Outlook.Application") 
outMail = OutApp$CreateItem(0)

outMail[["To"]] = "recipient's email address"
outMail[["subject"]] = "subject" 
outMail[["body"]] = paste("Hello!", "", "The below summarises xxx:", pandoc.table.return(data.frame(V1 = 1:5, V2 = LETTERS[1:5])), sep = "\n")

outMail$Send()



Wednesday, 14 October 2015

radarchart - 'fmsb' package


radarchart() in 'fmsb' package offer a plotting function that draws radar/spider chart, similar to stars() in base package.


library(fmsb) 

'zoo' package converts date values to year-month values.
 
library(zoo) 
 
dat<-as.data.frame(sunspot.month) 

dat$TS<-seq(as.yearmon("1749-01-01"), as.yearmon("2013-09-01"), by = 1/12)
colnames(dat)[1] <- "sunspot"


The decades and centuries were derived from the year-month field to be used for grouping variables as below.

dat$decade <- floor(as.numeric(format(dat$TS, "%Y"))/10)*10
dat$century <- floor(as.numeric(format(dat$TS, "%Y"))/100)*100
dat$month <- format(dat$TS, "%b")
dat$month <- factor(dat$month, levels = unique(dat$month))
  
 
I used 'reshape2' package to rearrange the data to the desired structure while doing the aggregation to obtain mean values.

library(reshape2) 

agg <- recast(data = dat,century~month, measure.var = "sunspot", mean)


radarchart() requires the input data to have the max value and min value in the first and second rows respectively.
  
MX <- c(NA, rep(max(agg[,-1]), ncol(agg)-1)) 
MN <- c(NA, rep(min(agg[,-1]), ncol(agg)-1)) 

agg <- rbind(MX, MN, agg)


To draw a radar chart:

radarchart(agg[,-1])
  



  
  





















To assign colours for different groups and to insert a legend:


COL<-colorRampPalette(c("red", "blue"))(nrow(agg)-2) 

radarchart(agg[,-1], pcol = COL) 
legend(2, 1, legend = levels(as.factor(agg$century)), title = "century", col = COL, seg.len = 2, border = "transparent", pch = 16, lty = 1)




To change the line colour in the background:


radarchart(agg[,-1], pcol = COL, cglcol = "grey80") 
legend(2, 1, legend = levels(as.factor(agg$century)), title = "century", col = COL, seg.len = 2, border = "transparent", pch = 16, lty = 1)
























To modify number of layers in the background (in this example from 5 to 10):

radarchart(agg[,-1], pcol = COL, cglcol = "grey80", seg = 10) 
legend(2, 1, legend = levels(as.factor(agg$century)), title = "century", col = COL, seg.len = 2, border = "transparent", pch = 16, lty = 1)
  






















To add a title:

radarchart(agg[,-1], pcol = COL, cglcol = "grey80", seg = 10, title = "sun spots") 
legend(2, 1, legend = levels(as.factor(agg$century)), title = "century", col = COL, seg.len = 2, border = "transparent", pch = 16, lty = 1)























To split the groups in to individual radar chart:


par(mfrow = c(2, 2)) 
for(i in 3:nrow(agg)){ 
radarchart(agg[c(1,2,i), -1], pcol = COL[i-2], cglcol = "grey80", seg = 10, title = paste("Century:", agg$century[i], sep=" ")) 
}









































To colour the regions/polygons of the above graphs:


par(mfrow=c(2, 2)) 
for(i in 3:nrow(agg)){ 
radarchart(agg[c(1, 2, i), -1], pcol = COL[i-2], cglcol = "grey80", seg = 10, title = paste("Century:", agg$century[i], sep=" "), pdensity = 20, pangle = 30, pfcol = COL[i-2]) 
}




















































3D Graph - Lattice Package



cloud() function in 'lattice' package provides 3-dimensional visualisation option

library(lattice)

cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species)
  






















  
  

  
Adding a title
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species")  
 























  


Modify axis  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5),y=list(cex=0.9,tck=0.5),z=list(cex=0.9,tck=0.5)))
  






















  
  


Modify frame style  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1")))
























  
  
Modify axis labels  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)))
    























      




Modify plotting style  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"))
























  



Insert legend/key  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),key=list(space="right",title="Species",cex.title=1,points=list(col=c("tan","goldenrod","brown"),cex=0.9,pch=16),text=list(lab=levels(iris$Species),cex=0.9)))
  






















  
  




Rotating the graph (displayed using 'gridExtra' package for better comparison):
  
library(gridExtra)  
    
- around x-axis  
#default g1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = -60 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-60,y=0))

#x rotates graph around x axis - i.e. moves viewing  angle up and down - e.g. for view from top (x=0) g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = 0",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=0))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = -30",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-30,y=0))


grid.arrange(g1,g3,g2,nrow=1,main="Rotating Around X-Axis")

  



























  

- around z-axis  
  
#z rotates graph around z axis - i.e. moves viewing  angle left and right - e.g. for view from front (z=0) 
g1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 40 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-60,y=0))

g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 20",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=20,x=-60,y=0))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 0",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=0,x=-60,y=0))

grid.arrange(g1,g2,g3,nrow=1,main="Rotating Around Z-Axis")
  























  
- around y-axis  
  
#y rotates graph around y axis - i.e. moves viewing  angle up and down but hinges on y-axisg1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 0 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=0))

g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 30",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=30))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 60",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=60))

grid.arrange(g1,g2,g3,nrow=1,main="Rotating Around Y-Axis")


  


































Friday, 18 September 2015

alarms, beeps and voice

The below is available in R's base package as a notification method, for example, to alert users that a long running script has finished.

alarm()


'beepr' package is available to provide more options for notification sounds

install.packages("beepr")
library(beepr)
beep()
beep(1)
beep(2)
beep(3)
.
.
.


In Mac, text to speech technique can be called from R

system("say Loop 1 completed")


for different voice options

system("say -v Tessa Loop 2 completed")




Wednesday, 22 July 2015

reshape2 package - similar function to pivot table in Excel


recast() in "reshape2" package offers a function that can work like a pivot table in Excel.



library(reshape2)
recast(data = CO2, Plant + Type ~ Treatment, measure.var = "uptake", sum)
   Plant        Type nonchilled chilled
1    Qn1      Quebec      232.6     0.0
2    Qn2      Quebec      246.1     0.0
3    Qn3      Quebec      263.3     0.0
4    Qc1      Quebec        0.0   209.8
5    Qc3      Quebec        0.0   228.1
6    Qc2      Quebec        0.0   228.9
7    Mn3 Mississippi      168.8     0.0
8    Mn2 Mississippi      191.4     0.0
9    Mn1 Mississippi      184.8     0.0
10   Mc2 Mississippi        0.0    85.0
11   Mc3 Mississippi        0.0   121.1
12   Mc1 Mississippi        0.0   126.0


Sometimes, the variables need to be treated as vector.

recast(data=CO2, CO2$Plant + CO2$Type ~ CO2$Treatment, measure.var = "uptake", sum)