Reputation: 189
I have a data frame (Counts of faults in MS Office over a number of years) which I am using to generate a sparktable successfully:
df_Office_final_sparktable
component faults time
Excel 2 2001
Excel 1 2002
Excel 5 2003
Excel 5 2004
Excel 5 2005
Excel 6 2006
Excel 0 2007
Excel 0 2008
Excel 0 2009
Excel 0 2010
Excel 0 2011
Excel 0 2012
PPT 1 2001
PPT 1 2002
PPT 1 2003
PPT 1 2004
PPT 2 2005
PPT 3 2006
PPT 0 2007
PPT 0 2008
PPT 0 2009
PPT 0 2010
PPT 0 2011
PPT 0 2012
Word 5 2001
Word 4 2002
Word 3 2003
Word 1 2004
Word 3 2005
Word 2 2006
Word 5 2007
Word 3 2008
Word 0 2009
Word 1 2010
Word 0 2011
Word 0 2012
Here is the corresponding code:
Office_content<-list(
2001=function(x) { head(x,1) },
2012=function(x) { tail(x,1) },
Office_content[['Office Trend']]<-newSparkLine())
Office_varType<-rep("outages",3) df_Office_final_sparktable<-df_Office_final_sparktable[,c("component","faults","time")] df_Office_final_sparktable$time<-as.numeric(as.character(df_Office_final_sparktable$time)) Office_dat<-reshapeExt(df_Office_final_sparktable,idvar="component",varying=list(2))
Office_sparkTab<-newSparkTable(Office_dat,Office_content,Office_varType)
showSparkTable (Office_sparkTab , outputType = "html", filename = "t1")
As you can see I am able to output a table of the 2001 and 2012 faults using the head commands and a corresponding sparkline. However I can't seem to figure out how to output the fault totals from 2002 - 2011 (inclusive) I tried to pull the second row from of the dataframe using the following code:
df_Office_final_sparktable[2:2, 1:3]
I realise this isn't correct as there is no way for me to map this command back to a function so i was wondering do i need to write 10 separate functions that i need to call in order to pull the required fault totals for each office component year on year?
Thanks in advance, Jonathan
Upvotes: 0
Views: 36
Reputation: 189
Ok i finally figured this one out. I had to create 10 functions which would pull the 2nd row from the data frame like so:
head_row2_Office <- function(x) {
result <- subset(x, time =='2002' & component =="Excel", select=c(1:3))
return (result)
}
After that i had to call the functions in the list function like so:
Office_content<-list(
Jan=function(x) { head(x,1) },
Feb=function(x) { head_row2_Office(x) },
Mar=function(x) { head_row3_Office(x) },
Apr=function(x) { head_row4_Office(x) },
May=function(x) { head_row5_Office(x) },
Jun=function(x) { head_row6_Office(x) },
Jul=function(x) { head_row7_Office(x) },
Aug=function(x) { head_row8_Office(x) },
Sep=function(x) { head_row9_Office(x) },
Oct=function(x) { head_row10_Office(x) },
Nov=function(x) { head_row11_Office(x) },
Dec=function(x) { tail(x,1) },
Office_content[['Office Trend']]<-newSparkLine()
)
Office_varType<-rep("outages",13) # set the tables columns to 13 for table + graph
df_Office_final_sparktable<-df_Office_final_sparktable[,c("component","faults","time")]
df_Office_final_sparktable$time<-as.numeric(as.character(df_Office_final_sparktable$time))
Office_dat<-reshapeExt(df_Office_final_sparktable,idvar="component",varying=list(2))
Office_sparkTab<-newSparkTable(Office_dat,Office_content,Office_varType)
showSparkTable (Office_sparkTab , outputType = "html", filename = "t1")
Upvotes: 0