0

I'm learning to use the sparkTable package. I'm creating tables having a sparkLine followed by a certain numbers of columns (not fixed). The columns all achieve the same thing: display the n'th element of the time series, rounded to 2 digits. For instance, column 1 following the spark graph will display the value of the 1st time point, 2nd column, the 2nd data point, and so on. For now, I deal with it by:

First declaring a list of functions allowing for as many time points I might expect to come up in the data.

get.values <- list(function(x) round(x[1],2),
                   function(x) round(x[2],2),
                   function(x) round(x[3],2),
                   function(x) round(x[4],2),
                   function(x) round(x[5],2),
                   function(x) round(x[6],2),
                   function(x) round(x[7],2),
                   function(x) round(x[8],2),
                   function(x) round(x[9],2),
                   function(x) round(x[10],2))

Then, when calling newSparkTable, I make the tableContent argument "expand" to the number of time points present in the data:

st <- newSparkTable(my.data, tableContent = c(list(newSparkLine()), 
                                              get.values[1:max(my.data$time)]),
                    ...)

This works well, but I'm thinking there must be a more flexible approach (I'm thinking of lambda functions in Python, or something like that). Ideas most welcome.

Edit

I'm getting closer to it. A way to have this code evaluated would do the trick:

paste("function(x) round(x[", 1:max(my.data$time), "],2)", sep="")

# For instance if max(my.data$time)==10
> paste("function(x) round(x[", 1:10, "],2)", sep="")
 [1] "function(x) round(x[1],2)"  "function(x) round(x[2],2)"  "function(x) round(x[3],2)" 
 [4] "function(x) round(x[4],2)"  "function(x) round(x[5],2)"  "function(x) round(x[6],2)" 
 [7] "function(x) round(x[7],2)"  "function(x) round(x[8],2)"  "function(x) round(x[9],2)" 
[10] "function(x) round(x[10],2)"

But then:

> eval(parse(text=paste("function(x) round(x[", 1:10, "],2)", sep="")))
function(x) round(x[10],2)

Only the last one gets eval'd. :s

4

1 回答 1

1

Use lapply with a factory function to create the list of functions:

round.factory = function(index) {
    j = index
    function(x) round(x[j], 2)
}

get.values = lapply(1:10, round.factory)

The intermediate variable j is necessary to to bind the value of index to the local scope.

于 2014-11-09T09:23:57.720 回答