R: title (or comment)

I want to put a comment under the table printed by xtable. . I decided that the best option would be to use the "caption" option: xtable(tablename, caption="This is a caption") . But it somehow enters "Table 1" automatically, so the output looks like this:

Table 1: This is an inscription.

Is there a way to suppress this or any easier way to put a comment simply as an additional last line in the table?

+11
source share
3 answers

Firstly, some layout data:

 x <- sample(LETTERS, 5, replace = TRUE) y <- sample(LETTERS, 5, replace = TRUE) z <- table(x, y) 

Now here are some awkward solutions using the print.xtable add.to.row argument.

 comment <- list() comment$pos <- list() comment$pos[[1]] <- c(nrow(z)) comment$command <- c(paste("\\hline \n", # we`ll replace all default hlines with this and the ones below "your footnote, caption or whatever. \n", sep = "")) print(xtable(z), add.to.row = comment, hline.after = c(-1, 0)) # indicates rows that will contain hlines (the last one was defined up there) 

If you want your comment to be placed before the data, use comment$pos[[1]] <- c(0) instead of comment$pos[[1]] <- c(nrow(z)) and configure hline.after .

Here is my conclusion:

 % latex table generated in R 2.14.1 by xtable 1.7-0 package % Mon Feb 20 02:17:58 2012 \begin{table}[ht] \begin{center} \begin{tabular}{rrrrr} \hline & B & C & P & V \\ \hline A & 0 & 0 & 0 & 1 \\ D & 1 & 0 & 0 & 0 \\ I & 0 & 0 & 0 & 1 \\ P & 0 & 0 & 1 & 0 \\ Z & 0 & 1 & 0 & 0 \\ \hline your footnote, caption or whatever. \end{tabular} \end{center} \end{table} xtable 1.7-0 package % latex table generated in R 2.14.1 by xtable 1.7-0 package % Mon Feb 20 02:17:58 2012 \begin{table}[ht] \begin{center} \begin{tabular}{rrrrr} \hline & B & C & P & V \\ \hline A & 0 & 0 & 0 & 1 \\ D & 1 & 0 & 0 & 0 \\ I & 0 & 0 & 0 & 1 \\ P & 0 & 0 & 1 & 0 \\ Z & 0 & 1 & 0 & 0 \\ \hline your footnote, caption or whatever. \end{tabular} \end{center} \end{table} 
+12
source

If you are using RMarkdown, add it to the header:

 --- (other configs here, like title, author, etc.) header-includes: - \usepackage{caption} - \captionsetup{labelformat=empty} --- 

Edit:

After talking with xxtable package maintainer David (it was very affordable), he came up with this solution, which I am posting below:

I think this can be solved with xtableList. Create some data and convert the data frame in xtableList.

 set.seed(230) DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5)) library(xtable) dfList <- list(DF) attr(dfList, "message") <- c("A caption", "Which can have multiple lines") 

Then xtable produces the following:

 print(xtableList(dfList)) ## % latex table generated in R 3.2.5 by xtable 1.8-3 package ## % Sat Jul 09 21:52:53 2016 ## \begin{table}[ht] ## \centering ## \begin{tabular}{rrrr} ## \hline ## & a & b & c \\ ## \hline ## 1 & -0.23 & 0.04 & 1.34 \\ ## 2 & 0.10 & 0.57 & -1.62 \\ ## 3 & 0.33 & -0.14 & 0.83 \\ ## 4 & 0.36 & -0.75 & 0.20 \\ ## 5 & 0.44 & 0.13 & -0.49 \\ ## \hline ## \multicolumn{4}{l}{A caption}\\ ## ## \multicolumn{4}{l}{Which can have multiple lines}\\ ## \end{tabular} ## \end{table} 

To deal with long titles, you need to split the string:

 attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines") 
+2
source

This basically repeats this answer, but this is the most programmatic way to do this with xtable . This is ugly, mainly because I hate the way xtable add.to.row .

Sample data:

 set.seed(230) DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5)) #of course, we can pass this directly below; I'm just saving # horizontal space for this answer comm <- paste0("\\hline \n \\multicolumn{4}{l}", "{\\scriptsize{Check out these random numbers!}} \n") print.xtable(xtable(DF, caption = "Describe the table"), #adjusting hline.after so that our comment appears # "outside" the table, as defined by its border hline.after=c(-1, 0), #**NOTE: the first argument to add.to.row must be # a list -- don't ask me why since it strikes me as odd** add.to.row = list(pos = list(5), command = comm)) why since it strikes me as odd ** set.seed(230) DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5)) #of course, we can pass this directly below; I'm just saving # horizontal space for this answer comm <- paste0("\\hline \n \\multicolumn{4}{l}", "{\\scriptsize{Check out these random numbers!}} \n") print.xtable(xtable(DF, caption = "Describe the table"), #adjusting hline.after so that our comment appears # "outside" the table, as defined by its border hline.after=c(-1, 0), #**NOTE: the first argument to add.to.row must be # a list -- don't ask me why since it strikes me as odd** add.to.row = list(pos = list(5), command = comm)) 

Here is the TeX output:

 % latex table generated in R 3.2.4 by xtable 1.8-2 package % Mon May 23 18:25:14 2016 \begin{table}[ht] \centering \begin{tabular}{rrrr} \hline & a & b & c \\ \hline 1 & -0.23 & 0.04 & 1.34 \\ 2 & 0.10 & 0.57 & -1.62 \\ 3 & 0.33 & -0.14 & 0.83 \\ 4 & 0.36 & -0.75 & 0.20 \\ 5 & 0.44 & 0.13 & -0.49 \\ \hline \multicolumn{4}{l}{\scriptsize{Check out these random numbers!}} \end{tabular} \caption{Describe the table} \end{table} 

And the result is .pdf if I wrap it with \documentclass{article} , \begin{document} and \end{document} :

enter image description here

Of course, there are many more bells and whistles to add to the readiness for publication, but this is the point, and you should be well on your way.

0
source

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


All Articles