Skip to content

Instantly share code, notes, and snippets.

@ivyleavedtoadflax
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivyleavedtoadflax/70d7d7a04ee3f81982d3 to your computer and use it in GitHub Desktop.
Save ivyleavedtoadflax/70d7d7a04ee3f81982d3 to your computer and use it in GitHub Desktop.
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{Info}
Solution to \href{http://stackoverflow.com/questions/30309311/error-in-applying-a-for-loop-to-an-xtable-in-knitr}{this} Stack Overflow post. Tested with \texttt{xtable} version 1.7-4 on:
\begin{itemize}
\item{R version 3.2.0 (2015-04-16)}
\item{Platform: x86\_64-pc-linux-gnu (64-bit)}
\item{Running under: Ubuntu 14.04.2 LTS}
\end{itemize}
\section{Recreate \texttt{data.frame}}
<<>>=
library(xtable)
df.2 <- data.frame(
DATE = c("2043:3","2043:4","2044:1","2044:2","2044:3","2044:4"),
CY = c(rep(2043,2),rep(2044,4)),
FY = c(rep(2044,4),rep(2045,2)),
Quarter = c(3,4,1,2,3,4),
XEMPNIN = c(3324.391,3326.214,3328.492,3330.100,3331.963,3334.248)
)
@
\section{Alter \texttt{bold()} function}
<<>>==
bold <- function(x, matrix) {
x[] <- lapply(x, as.character)
for (i in 1:ncol(x)) {
yes <- matrix[,i]
x[yes,i] <- paste('\\textbf{', x[yes,i], '}', sep = "")
}
# change print() for return()
return(x)
}
@
\section{Run OP code}
<<>>=
l.vec <- as.logical(c(1,1,1,1,1,
1,1,1,1,1,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,1))
l.mat <- matrix(l.vec, nrow = 6, ncol = 5, byrow = TRUE)
x.df.2 <- (xtable(df.2))
x.df.3 <- bold(x.df.2, l.mat)
@
\section{Print table}
Note \texttt{<<results="asis">>=} in chunk options:
<<results="asis">>=
print.xtable(
x.df.3,
include.rownames = FALSE,
sanitize.text.function = identity
)
@
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment