rJava, rCharts and R code to display GC data

These are the steps I follow to display GC activity data using a nvd3 discrete bar chart.

Call Java class using rJava

  gctypes <- .jcall(realtimegcdataobserver ,"Ljava/util/List;","getGCTypes")

Create an empty data frame to hold the data

I get the type of the GC algorithm, GC count and time from JMX. I have yet to explore the last two values.

gcdata <- function(){
  df <- data.frame(

Iterate over the list of beans

Call appropriate methods and fill up the empty data frame.
I massage the data using the last two lines but don’t know any elegant way to accomplish this.

  emptygcdata <- gcdata()
  gctypedetails <- sapply( gctypes, function(item) rbind(emptygcdata, as.data.frame(c(GCType=item$getName(),Count=item$getL(),Time=item$getM()))))

  gctypedetails <- data.frame(gctypedetails)
  gctypedetails <- data.frame(matrix(unlist(gctypedetails)))

1 PS Scavenge
2 16
3 22
4 PS MarkSweep
5 0
6 0

emptygcdata <- gcdata()
  before <- 0
  after <- 2
    if (after >= nrow(gctypedetails))
    emptygcdata <- rbind(emptygcdata, data.frame(GCType =gctypedetails[before + 1,1], Count =gctypedetails[before + 2,1], Time=gctypedetails[before + 3,1]))
    before <- after + 1;
    after <- after + 2;

 GCType          Count  Time
1  PS Scavenge      16  22
2  PS MarkSweep     0  0

nvd3 using rCharts

  p2 = nPlot(x = "Time", y = "Count", data = emptygcdata, type = "discreteBarChart")
    color = "#! function(d){
      var ourColorScale = d3.scale.ordinal().domain(['PS MarkSweep','PS Scavenge']).range(['green','purple']);
      return ourColorScale(d.GCType);

rCharts and nvd3

I have been playing with some JavaScript chart libraries including nvd3 . These JS libraries are all wrapped by R code in the form of rCharts.

I have a data frame like this.

Var1 Freq
levelI 13
levelII 13
levelIII 12
levelIV 12

I want to plot a discreet bar chart using nvd3 and each bar should be of a different color depending on Var1. Var1 will have duplicate values.

#The type of <em>Var1</em> is integer in my data frame. So instead of debugging that
#I just convert it to "character" using this line.
bpf3$Var1 <- paste(bpf3$Var1,"",sep="")

#Match the first column with a hard-coded value and append a color
for(i in 1:nrow(bpf3)) {
      bpf3[i,1] <- paste(bpf3[i,1],"green",sep=":") 
      bpf3[i,1] <- paste(bpf3[i,1],"orange",sep=":") 
p2 = nPlot(x = "Var1", y = "Freq", data = bpf3, type = "discreteBarChart")

p2$chart(color = "#! function(d, x){ var color = d.Var1; return color.split(':')[1];} !#")

So it is possible to use a function that assigns a color to a bar in nvd3. rCharts lets you define a function like this.

The result is this. This is not an elegant way though.

Screen Shot 2014-06-26 at 12.14.19 PM

Update :

The author of http://timelyportfolio.blogspot.in/ pointed out that there is a function that can be used like this.

    color = "#! function(d){
      var ourColorScale = d3.scale.ordinal().domain(['levelI','levelII']).range(['red','blue']);
      return ourColorScale(d.Event);

It works splendidly.