Customize Xtable

Customize xtable

You need some pre-processing, extra argument passed to print.xtable and some post-processing:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
Values1 = c("N=10", 1.03, 1.71, 2.25),
Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

# Pre-processing: rotates column names by 45 degrees
head = apply(as.array(names(my.table)), 1, function(x) paste("\\rotatebox{45}{", x, "}"))
head = paste(head, c(rep("&", length(head)-1), "\\\\\n"), collapse="")

latex.tab <- xtable(my.table, caption=c("Stats"))
ltable = print(latex.tab, file="", # File is empty, post-processing needed
floating.environment='sidewaystable',
include.rownames=FALSE,
include.colnames=FALSE, # No colnames
booktabs=TRUE,
latex.environment="center", # Or NULL
# Adds some extra-text after the rows specified in pos.
# Adds new \midrule and comments old one.
# Adds pre-processed names of columns
add.to.row=list(pos=as.list(c(0, 0, 1)), command=as.vector(c(head, "%", "\\midrule\n"))))

# Post-processing: replaces \begin{center} with \centering
ltable = sub("\\begin{center}\n", "\\centering\n", ltable, fixed=TRUE)
ltable = sub("\\end{center}\n", "\n", ltable, fixed=TRUE)

# Post-processing: adds alternating colours
ltable = sub("\\begin{tabular}",
"\\rowcolors{2}{gray!25}{white}\n\\begin{tabular}",
ltable, fixed=TRUE)

# Writes output to the file
cat(ltable, file="Summarystats.tex")

If you need other tabs environment than tabular you can
1) add new variable:

TABULAR = "tabular"

2) Pass it's value to print.xtable like this:

...
tabular.environment=TABULAR,
...

3) Change your post-processing for alternating colors:

ltable = sub(sprintf("\\begin{%s}", TABULAR),
sprintf("\\rowcolors{2}{gray!25}{white}\n\\begin{%s}", TABULAR),
ltable, fixed=TRUE)

Result:

Sample Image

Change variable name in xtable

I have always found it easiest to pass a matrix to xtable; the options in declaring a matrix include dimnames, which makes it easy to print out whatever you'd like:

print(xtable(matrix(tab,dimnames=list(names(tab),"Whatever You'd Like")),
caption="Conversion a Premium (en tanto por ciento)",
label="table:Conversion",
digits=2),latex.environments = "center"
)

Produces:

% latex table generated in R 3.2.1 by xtable 1.7-4 package
% Mon Aug 17 12:01:18 2015
\begin{table}[ht]
\centering
\begin{tabular}{rr}
\hline
& Whatever You'd Like \\
\hline
No se Convierte & 99.20 \\
Se Convierte & 0.80 \\
\hline
\end{tabular}
\caption{Conversion a Premium (en tanto por ciento)}
\label{table:Conversion}
\end{table}

How to change column heading using xtable in R?

xtable inherits data.frame.

So,

library(xtable)
xt <- xtable(tableData)

names(xt) <- c('Height','Width','Breadth' )

will work.

xtable: change the color and thickness (table.attributes)

This seems to be the preferred way to get your desired format, thanks to @user20650

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL,
include.rownames=FALSE, include.colnames=FALSE)

# \begin{tabular}{lll|ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}

attr(out_table, "align") <-
c("l", "l","l","!{\\color[HTML]{BDBDBD}\\vrule width .25pt}","l","l")
print(out_table, floating = FALSE, hline.after = NULL,
include.rownames=FALSE, include.colnames=FALSE)

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}
@

\end{document}

Results with

Sample Image

And some other hackier options:

All this amounts to is subbing out {lll|ll} for {lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}

And you need the xcolor package to use your hex color, #BDBDBD, and colortbl for the colored vrule

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll|ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}

cat(gsub(paste0(attr(out_table, 'align'), collapse = ''),
'lll!{\\color[HTML]{BDBDBD}\\vrule width .25pt}ll',
print(out_table, floating = FALSE, hline.after = NULL,
print.results = FALSE), fixed = TRUE))

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}
@

\end{document}

Gives me this

Sample Image

Alternatively, if something like this works, it would be much more simple as pointed out by @user20650 (although I tried something similar at first and it was finicky for me about the alignments, but I probably just did something wrong)

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll|ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}

attr(out_table, 'align') <-
'lll!{\\color[HTML]{BDBDBD}\\vrule width .25pt}ll'
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
# & 1 & 2 & 3 & 4 \\
# 1 & SUNE & Apr 01 & EXL & Mar 18 \\
# 2 & WST & & VG & \\
# \end{tabular}
@

\end{document}

And you still get the same results:

Sample Image

set different Digits in xtable

You may define the digits for each column. Your table has 12 columns. So the first 11 columns get 2 digits and the 12th column gets 0 digits.

xtable::xtable(head(mtcars), digits = c(rep(2, 11), 0))

Result

% latex table generated in R 3.6.3 by xtable 1.8-4 package
% Mon Oct 12 10:25:04 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrr}
\hline
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4 \\
Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\
Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1 \\
Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2 \\
Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\
\hline
\end{tabular}
\end{table}

Problems forming a LaTeX table using xtable-package in R

Here's my approach (with inspiration from)

library(xtable)
financial <- c(1.23, 1.19)
macro <- c(1.50, 1.40)
X <- rbind(financial, macro)
colnames(X) <- c("A","B")

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 0
addtorow$pos[[6]] <- 0
addtorow$pos[[7]] <- 0
addtorow$command <- c('Country: & United States & \\\\\n',
'\\hline',
'Method: & Regression & \\\\\n',
'\\hline',
'Models: & RMSE Result & \\\\\n',
'\\hline',
'& A & B \\\\\n')

print(xtable(X), add.to.row = addtorow, include.colnames = FALSE )

add.to.row can be used to include custom latex code into xtable.

I did could not figure out how to place your column names & A & B after the custom header. My solution for this was to use include.colnames = FALSE and manually inserted column headers in add.to.row. (perhaps someone else has a more elegant solution?)

Result:

% latex table generated in R 4.0.5 by xtable 1.8-4 package
% Mon Jan 17 14:16:51 2022
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
Country: & United States & \\
\hline Method: & Regression & \\
\hline Models: & RMSE Result & \\
\hline & A & B \\
\hline
financial & 1.23 & 1.19 \\
macro & 1.50 & 1.40 \\
\hline
\end{tabular}
\end{table}

Sample Image

using gsub to modify output of xtable command

What about cat(x,sep='\n') instead of print(x)



Related Topics



Leave a reply



Submit