Question

I am using knitr and xtable to automate my reporting procedure. I want to highlight a few rows of a table and have a horizontal line right above each row highlighted. The .Rnw file I am using reads as below:

\usepackage{colortbl, xcolor}
\usepackage{longtable}

\begin{document}

<<do_table, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))

print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2))
@

\end{document}

This works just fine, however, the table I am working with should be a longtable, if I adjust the last line of code to

print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2),tabular.environment="longtable",floating=FALSE)

the output is quite ugly, and the rows are not highlighted as expected. Anyone might know an answer to this question?

thanks,

David

Was it helpful?

Solution 2

You are on the right track, but I am a bit confused: do you want the selected rows highlighted by hline and rowcolor? In my experience, rowcolor alone looks better, so I will assume that in my answer below (but you could easily use both, just append the \\hline command).

As a bonus, all code below assumes you use the LaTeX booktabs package, which gives correctly weighted rules (unlike hline). To be honest, I always work with booktabs, and I couldn't bother to adjust the code to use hline -- but if you prefer hline, replace all \toprule, \midrule and \bottomrule macros with \hline.

You seem to have missed that LaTeX longtables require a special header, and we need to supply that too as an element to the command vector of the add.to.row list (this may be the reason your typeset table looks bad).

longtable.xheader <-
   paste("\\caption{Set your table caption.}",
     "\\label{tab:setyourlabel}\\\\ ",
     "\\toprule ",
     attr(xtable(mydf), "names")[1],
     paste(" &", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""),
     "\\\\\\midrule ",
     "\\endfirsthead ",
     paste0("\\multicolumn{", ncol(xtable(mydf)), "}{c}{{\\tablename\\ \\thetable{} -- continued from previous page}}\\\\ "),
     "\\toprule ",
     attr(xtable(mydf), "names")[1],
     paste("&", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""),
     "\\\\\\midrule ",
     "\\endhead ",
     "\\midrule ",
     paste0("\\multicolumn{", as.character(ncol(xtable(mydf))), "}{r}{{Continued on next page}}\\\\ "),
     "\\bottomrule \\endfoot ",
     "\\bottomrule \\endlastfoot ",
     collapse = "")

With that taken care of, go ahead and print the xtable:

print(xtable(mydf), 
      floating = FALSE, % since longtable never floats
      hline.after = NULL, % hline off since I use booktabs
      add.to.row = list(pos = list(-1, 
                               c(0, 2),
                               nrow(xtable(mydf))),
                    command = c(longtable.xheader, 
                                "\\rowcolor[gray]{0.75}\n",
                                "%")), % comments out a spurious \hline by xtable
      include.rownames = FALSE, % depends on your preference
      include.colnames = FALSE, % depends on your preference
      type = "latex", 
      tabular.environment = "longtable",
      % xtable tries to escape TeX special chars, can be annoying sometimes
      sanitize.text.function = function(x){x},
      % not all dashes are meant to be math negative sign, set according to your data
      math.style.negative = FALSE)

I hope my use of booktabs in the answer did not confuse you too much. Keep knitting!

OTHER TIPS

Sorry, slightly offtopic, but demonstrating a markdown-only solution for highlighting cells/rows easily:

> mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
> library(pander)
> emphasize.strong.rows(c(1, 3))
> pander(mydf)

---------------------------
 id      var1       var2   
----- ---------- ----------
**1** **0.7194** **0.6199**

  2     0.8094     0.1392  

**3** **-1.254** **0.5308**

  4     0.4505     0.8235  

  5    -0.3779     0.7534  

  6    -0.3518     0.3055  

  7     1.759      0.5366  

  8     0.9822     0.9938  

  9     1.549      0.3589  

 10     -1.077     0.5153  
---------------------------

That can be converted to LaTeX or pdf directly.

You might have more luck posting this on a latex forum. You should note that xcolor/longtable are not compatible: http://www.ukern.de/tex/xcolor.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top