I am working with the R package "sparkTable" to create some tables in markdown for a PDF report. I have the table and the charts I want, but I don't seem to see any provisions for formatting the text inside the table. On things like kable and pander you can format the positioning of the text, just don't seem to see a way to do in with sparkTable. Maybe there is some LaTeX way of doing this. If anyone has any ideas, I'd be greatful. Thanks!
R sparkTable Formatting - Center Text
357 Views Asked by azdatasci At
2
There are 2 best solutions below
0

Maybe inline CSS is an option:
library(sparkTable)
data(pop,package="sparkTable")
content <- list(
function(x) {
x <- round(mean(x),2)
ifelse(x>1000, sprintf('<span style="color:#ff0000">%s</span>', x), # red numbers
sprintf('<span style="color:#0000ff">%s</span>', x)) # blue numbers
},
newSparkBox(),
newSparkLine(),
newSparkBar(),
function(x) { round(tail(x,1),2) }
)
names(content) <- paste("column",1:length(content),sep="")
varType <- rep("value",length(content))
pop <- pop[,c("variable","value","time")]
pop$time <- as.numeric(as.character(pop$time))
xx <- reshapeExt(pop,idvar="variable", varying=list(2))
x1 <- newSparkTable(xx, content, varType)
showSparkTable(x1)
Ok -I figured this out. Turns out sparkTable uses the xtable package in R to format the output - the default, once converted to LaTeX was to have the alignment in the section for the table be:
\begin{tabular}{rllll}
This was with no options passed. I found the "methods.R" script in the package source and added the option:
align = 'rccccc'
to the xtable options in two places (lines 1287 and 1314):
1287: print(xT <- xtable(m, align='rcccc'), sanitize.text.function = function(x){x},comment=infonote)
1314: print(xT <- xtable(m, align='rcccc'), sanitize.text.function = function(x){x})
Once that was done, I built the package and reinstalled it. I re-ran the knit to pdf on the markdown and voila! My data was centered in those later columns.
I know this is more of a custom solution, but I am only using this for a set amount of data and it works for my needs. Since sparkTable uses xtable within its own code, there was no way for me to pass these options (that I know of), so I coded it in the source. It would be nice, if in future version, that centering and formatting was allowed at the sparkTable level, but this works for now.
Now I just need to figure out fonts and I'm all set. Thanks for the feedback, the solution above did't work directly, but pointed me in the right direction. Thanks!