[R] odfTable

Sarah Goslee sarah.goslee at gmail.com
Tue Jun 5 17:09:17 CEST 2007


Hi,

There are a couple of steps, and it requires use of the newest version of
odfWeave.

First, you need to set up the table styles in R _before_ you run odfWeave.

Here's an example:

	# Now to specify the styles themselves
	# The default list has 10 styles in it right now.
	existingStyles <- getStyleDefs()

	# Use one of the cell styles as a template for the new cell style
	# The style definition contains information on background
	# color, alignment, and borders.
	# For style 1, I want a background color.
	# Colors can be specified in hex format, or as "transparent".
	newStyle1 <- existingStyles$noBorder
	newStyle1$backgroundColor <- "#cccccc"

	# Finally, the new styles need to be set.
	existingStyles <- c(existingStyles, newStyle1=list(newStyle1))
	setStyleDefs(existingStyles)

After that, you can use the new style when defining your table in an
odfWeave document:

<<Table1,echo=FALSE,results=xml>>=
	### Conditional formatting based on the value of the table cell
	x <- data.frame(Var1=1:3, Var2=4:6, Var3=7:9, row.names=c("A", "B", "C"))

	# Here's a matrix specifying the desired styles for each cell in the table.
	# This could be p-values for the correlations in x, for example. For
	# this example, I'm just making it up.
	# The style matrix must have an additional column for the row names.
	y <- matrix(c(0, 0, 0, 1,0,0,3,0,1,2,0,2), byrow=FALSE, nrow=3, ncol=4)
	y <- data.frame(y)
	colnames(y) <- c("rownames", "Var1", "Var2", "Var3")

	# Now, to specify when to use these new styles.
	# tableStyles() will provide the default styles for each
	# element of x
	# note that I'm creating the styles for x based on y,
	# which already has the extra column for row names.
	x.styles <- tableStyles(y, header=colnames(y))

	# every element of x has two associated styles
	# for the cells, text style is in text and cell style is in cell
	# for headers, it is in header and headerCell
	
	# newStyle1 and newStyle2 are cell styles, so they are set here:
	x.styles$cell[y == 1] <- "newStyle1"

	odfTable(x, useRowNames = TRUE, styles = x.styles)
@

I have a more detailed explanation that I can send off-list if you are
interested. For your specific problem, you'd need to set the headerCell
style.

Sarah


On 6/5/07, Laurence Amilhat <Laurence.Amilhat at toulouse.inra.fr> wrote:
> Hello,
>
> I am using the odfWeave packages;
> I draw a table using the function odfTable:
>
> partCols <- gsub("\\.", " ", names(partenaires))
> odfTable(partenaires, useRowNames = FALSE, colnames=partCols)
>
>
> it's working as I have a table in my output file.
> I would like to know how to change the background color for the header
> cells.
> I assuming i have to use tableStyles, but I don't understand how.
>
> Does someone have an idea to help me?
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list