Formatting Sweave Tables, Two Problems

I am struggling with two formatting problems in Sweave. One of them is italics for the text in the table, the second is how to add a row to add columns to the bottom of the table. See the next MWE for more details.

\documentclass[12pt,letterpaper,oneside]{amsart} \usepackage{graphicx} \usepackage{longtable} \SweaveOpts{keep.source=TRUE} % Keeps comments in the R code. % \begin{document} % \maketitle \section*{The Challenge} Italicize the taxa in the following table and add a row at the bottom which gives the sum of the sites column. <<echo=TRUE>>= # Creating Data taxa <- c("Arabidopsis thaliana", "Populus trichocarpa", "Brachypodium distachyon") sites <- c(270,320,240) data.df <- data.frame(taxa,sites) @ ~ <<label=tab1,echo=TRUE,results=tex>>= # Creating Table library(xtable) data.table <- xtable(data.df, caption="Sweave Output") print(data.table, caption.placement="top") @ The results should look something like table 2, which was hand coded in \LaTeX. \begin{table}[ht] \caption{Manually coded \LaTeX} \begin{tabular}{llr} \hline & taxa & sites \\ \hline 1 & \emph{Arabidopsis thaliana} & 270 \\ 2 & \emph{Populus trichocarpa} & 320 \\ 3 & \emph{Brachypodium distachyon} & 240 \\ \hline & Total Sites & 830 \\ \hline \end{tabular} \end{table} \end{document} 
+4
source share
1 answer

Add \emph manually with paste and add a new line with rbind , specifying the new line name in one place. Also, use stringsAsFactors=FALSE in the creation of the original data frame to make it possible to add a new value to the columns of taxa.

Then use sanitize.text.function=identity to xtable to keep the backslash in the \emph and hline.after to get the lines you want in.

 \documentclass[12pt,letterpaper,oneside]{amsart} \usepackage{graphicx} \usepackage{longtable} \SweaveOpts{keep.source=TRUE} % Keeps formatting of the R code. \begin{document} <<echo=TRUE>>= taxa <- c("Arabidopsis thaliana", "Populus trichocarpa", "Brachypodium distachyon") sites <- c(270,320,240) data.df <- data.frame(taxa,sites, stringsAsFactors=FALSE) @ ~ <<label=tab2, echo=TRUE, results=tex>>= library(xtable) data.df$taxa <- paste("\\emph{",taxa,"}", sep="") data.df <- rbind(data.df, ` `=c("Total Sites", sum(data.df$sites))) data.table <- xtable(data.df, caption="Sweave Output") print(data.table, caption.placement="top", sanitize.text.function=identity, hline.after=c(-1,0,nrow(data.df)-1, nrow(data.df))) @ \end{document} 
+4
source

Source: https://habr.com/ru/post/1379489/


All Articles