diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index 0dca4bb..5d89fbc 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -296,7 +296,9 @@ print.xtable <- function(x, "}\n"), sep = "", collapse = ""), sep = "") - + ## fix 05-26-22 + BLABEL <- "\\label{" + ELABEL <- "}\n" ## fix 10-26-09 (robert.castelo@upf.edu) the following ## 'if' condition is added here to support ## a caption on the top of a longtable @@ -306,11 +308,17 @@ print.xtable <- function(x, } else { BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "") } - ECAPTION <- "} \\\\ \n" + ECAPTION <- "} \n" + EHEADER <- "\\\\" if ((!is.null(caption)) && (type == "latex")) { BTABULAR <- paste(BTABULAR, BCAPTION, caption, ECAPTION, sep = "") } + if ((!is.null(attr(x, "label", exact = TRUE))) && (type == "latex")) { + BTABULAR <- paste(BTABULAR, BLABEL, attr(x, "label", exact = TRUE), ELABEL, + sep = "") + } + BTABULAR <- paste(BTABULAR,EHEADER,sep = "") } ## Claudio Agostinelli dated 2006-07-28 ## add.to.row position -1 @@ -340,8 +348,6 @@ print.xtable <- function(x, BSIZE <- paste("\\begingroup", size, "\n", sep = "") ESIZE <- "\\endgroup\n" } - BLABEL <- "\\label{" - ELABEL <- "}\n" ## Added caption width (jeff.laake@nooa.gov) if(!is.null(caption.width)){ BCAPTION <- paste("\\parbox{",caption.width,"}{",sep="") @@ -425,13 +431,14 @@ print.xtable <- function(x, if ( floating == TRUE ) { if ((!is.null(caption)) && (type == "html" ||caption.placement == "top")) { - result <- result + BCAPTION + as.string(caption) + ECAPTION + result <- result + BCAPTION + caption + ECAPTION } if (!is.null(attr(x, "label", exact = TRUE)) && (type == "latex" && caption.placement == "top")) { result <- result + BLABEL + attr(x, "label", exact = TRUE) + ELABEL } + } result <- result + BSIZE result <- result + BTABULAR @@ -582,13 +589,14 @@ print.xtable <- function(x, ## that bottom caption interferes with a top caption of a longtable if(caption.placement == "bottom"){ if ((!is.null(caption)) && (type == "latex")) { - result <- result + BCAPTION + as.string(caption) + ECAPTION - } + result <- result + BCAPTION + caption + ECAPTION } + if (!is.null(attr(x, "label", exact = TRUE))) { result <- result + BLABEL + attr(x, "label", exact = TRUE) + ELABEL } + } ETABULAR <- "\\end{longtable}\n" } result <- result + ETABULAR @@ -596,7 +604,7 @@ print.xtable <- function(x, if ( floating == TRUE ) { if ((!is.null(caption)) && (type == "latex" && caption.placement == "bottom")) { - result <- result + BCAPTION + as.string(caption) + ECAPTION + result <- result + BCAPTION + caption + ECAPTION } if (!is.null(attr(x, "label", exact = TRUE)) && caption.placement == "bottom") { @@ -633,19 +641,17 @@ string <- function(text, file = "", append = FALSE) { } as.string <- function(x, file = "", append = FALSE) { - if (is.string(x)) { + if (is.null(attr(x, "class", exact = TRUE))) + switch(data.class(x), + character = return(string(x, file, append)), + numeric = return(string(as.character(x), file, append)), + stop("Cannot coerce argument to a string")) + if (class(x) == "string") return(x) - } else if (is.character(x)) { - return(string(x, file, append)) - } else if (data.class(x) == "numeric") { - return(string(as.character(x), file, append)) - } else { stop("Cannot coerce argument to a string") } -} - is.string <- function(x) { - return(is(x, "string")) + return(class(x) == "string") } diff --git a/pkg/R/xtableList.R b/pkg/R/xtableList.R index 2402338..b36a83e 100644 --- a/pkg/R/xtableList.R +++ b/pkg/R/xtableList.R @@ -46,7 +46,6 @@ print.xtableList <- function(x, hline.after = NULL, NA.string = getOption("xtable.NA.string", ""), include.rownames = getOption("xtable.include.rownames", TRUE), - include.colnames = getOption("xtable.include.colnames", TRUE), colnames.format = "single", only.contents = getOption("xtable.only.contents", FALSE), add.to.row = NULL, @@ -106,22 +105,21 @@ print.xtableList <- function(x, } if (colnames.format == "single"){ - ##add.to.row <- list(pos = NULL, command = NULL) - pos <- as.list(c(0, combinedRowNums[-length(x)], + add.to.row <- list(pos = NULL, command = NULL) + add.to.row$pos <- as.list(c(0, combinedRowNums[-length(x)], dim(combined)[1])) - comm <- unlist(sapply(x, attr, "subheading")) - command <- vector("character", length(x)) + command <- sapply(x, attr, "subheading") for (i in 1:length(x)){ - if( !is.null(comm[i]) ){ - command[i] <- + if( !is.null(command[[i]]) ){ + add.to.row$command[i] <- paste0(mRule,"\n\\multicolumn{", nCols + include.rownames, "}{l}{", - comm[i], + command[[i]], "}\\\\\n") } else { - command[i] <- paste0(mRule, "\n") + add.to.row$command[i] <- paste0(mRule, "\n") } } ## Changed at request of Russ Lenth @@ -132,20 +130,16 @@ print.xtableList <- function(x, attr(x, "message")[1] <- paste0("\\rule{0em}{2.5ex}", attr(x, "message")[1]) } - command[length(x) + 1] <- + add.to.row$command[length(x) + 1] <- paste0("\n\\multicolumn{", nCols + include.rownames, "}{l}{", attr(x, "message"), "}\\\\\n", collapse = "") - command[length(x) + 1] <- - paste0(bRule, command[length(x) + 1]) + add.to.row$command[length(x) + 1] <- + paste0(bRule, add.to.row$command[length(x) + 1]) - add.to.row$pos <- c(add.to.row$pos, pos) - add.to.row$command <- c(add.to.row$command, command) class(combined) <- c("xtableList", "data.frame") hline.after <- c(-1) - ## Allow use of own colnames, support request from - ## Seunghoon Lee, 26 May 2020 - ##include.colnames <- TRUE + include.colnames <- TRUE } ## Create headings for columns if multiple headings are needed @@ -165,14 +159,14 @@ print.xtableList <- function(x, colHead <- paste0(tRule, "\n", colHead, " \\\\", mRule, "\n") add.to.row <- list(pos = NULL, command = NULL) add.to.row$pos <- as.list(c(0, c(combinedRowNums[1:length(x)]))) - command <- unlist(sapply(x, attr, "subheading")) + command <- sapply(x, attr, "subheading") add.to.row$command[1] <- - if( !is.null(command[1]) ){ + if( !is.null(command[[1]]) ){ add.to.row$command[1] <- paste0("\n\\multicolumn{", nCols + include.rownames, "}{l}{", - command[1], + command[[1]], "}\\\\ \n", colHead, "\n") } else { add.to.row$command[1] <- colHead @@ -180,7 +174,7 @@ print.xtableList <- function(x, for (i in 2:length(x)) { add.to.row$command[i] <- - if( !is.null(command[i]) ) { + if( !is.null(command[[i]]) ) { paste0(bRule, "\\\\ \n\\multicolumn{", nCols + include.rownames, "}{l}{", diff --git a/pkg/tests/test.xtable.print.R b/pkg/tests/test.xtable.print.R new file mode 100644 index 0000000..8f8aeda --- /dev/null +++ b/pkg/tests/test.xtable.print.R @@ -0,0 +1,167 @@ +library(xtable) +data("iris") +xt <-xtable(iris, caption="iris",label = "tab:irirs") +print(xt , tabular.environment="longtable", floating = FALSE, include.rownames=FALSE, caption.placement = "top") + +## Output should be +# % latex table generated in R 4.0.4 by xtable 1.8-4 package +# % Thu May 26 10:35:07 2022 +# \begin{longtable}{rrrrl} +# \caption{iris} +# \label{tab:irirs} +# \\ \hline +# Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ +# \hline +# 5.10 & 3.50 & 1.40 & 0.20 & setosa \\ +# 4.90 & 3.00 & 1.40 & 0.20 & setosa \\ +# 4.70 & 3.20 & 1.30 & 0.20 & setosa \\ +# 4.60 & 3.10 & 1.50 & 0.20 & setosa \\ +# 5.00 & 3.60 & 1.40 & 0.20 & setosa \\ +# 5.40 & 3.90 & 1.70 & 0.40 & setosa \\ +# 4.60 & 3.40 & 1.40 & 0.30 & setosa \\ +# 5.00 & 3.40 & 1.50 & 0.20 & setosa \\ +# 4.40 & 2.90 & 1.40 & 0.20 & setosa \\ +# 4.90 & 3.10 & 1.50 & 0.10 & setosa \\ +# 5.40 & 3.70 & 1.50 & 0.20 & setosa \\ +# 4.80 & 3.40 & 1.60 & 0.20 & setosa \\ +# 4.80 & 3.00 & 1.40 & 0.10 & setosa \\ +# 4.30 & 3.00 & 1.10 & 0.10 & setosa \\ +# 5.80 & 4.00 & 1.20 & 0.20 & setosa \\ +# 5.70 & 4.40 & 1.50 & 0.40 & setosa \\ +# 5.40 & 3.90 & 1.30 & 0.40 & setosa \\ +# 5.10 & 3.50 & 1.40 & 0.30 & setosa \\ +# 5.70 & 3.80 & 1.70 & 0.30 & setosa \\ +# 5.10 & 3.80 & 1.50 & 0.30 & setosa \\ +# 5.40 & 3.40 & 1.70 & 0.20 & setosa \\ +# 5.10 & 3.70 & 1.50 & 0.40 & setosa \\ +# 4.60 & 3.60 & 1.00 & 0.20 & setosa \\ +# 5.10 & 3.30 & 1.70 & 0.50 & setosa \\ +# 4.80 & 3.40 & 1.90 & 0.20 & setosa \\ +# 5.00 & 3.00 & 1.60 & 0.20 & setosa \\ +# 5.00 & 3.40 & 1.60 & 0.40 & setosa \\ +# 5.20 & 3.50 & 1.50 & 0.20 & setosa \\ +# 5.20 & 3.40 & 1.40 & 0.20 & setosa \\ +# 4.70 & 3.20 & 1.60 & 0.20 & setosa \\ +# 4.80 & 3.10 & 1.60 & 0.20 & setosa \\ +# 5.40 & 3.40 & 1.50 & 0.40 & setosa \\ +# 5.20 & 4.10 & 1.50 & 0.10 & setosa \\ +# 5.50 & 4.20 & 1.40 & 0.20 & setosa \\ +# 4.90 & 3.10 & 1.50 & 0.20 & setosa \\ +# 5.00 & 3.20 & 1.20 & 0.20 & setosa \\ +# 5.50 & 3.50 & 1.30 & 0.20 & setosa \\ +# 4.90 & 3.60 & 1.40 & 0.10 & setosa \\ +# 4.40 & 3.00 & 1.30 & 0.20 & setosa \\ +# 5.10 & 3.40 & 1.50 & 0.20 & setosa \\ +# 5.00 & 3.50 & 1.30 & 0.30 & setosa \\ +# 4.50 & 2.30 & 1.30 & 0.30 & setosa \\ +# 4.40 & 3.20 & 1.30 & 0.20 & setosa \\ +# 5.00 & 3.50 & 1.60 & 0.60 & setosa \\ +# 5.10 & 3.80 & 1.90 & 0.40 & setosa \\ +# 4.80 & 3.00 & 1.40 & 0.30 & setosa \\ +# 5.10 & 3.80 & 1.60 & 0.20 & setosa \\ +# 4.60 & 3.20 & 1.40 & 0.20 & setosa \\ +# 5.30 & 3.70 & 1.50 & 0.20 & setosa \\ +# 5.00 & 3.30 & 1.40 & 0.20 & setosa \\ +# 7.00 & 3.20 & 4.70 & 1.40 & versicolor \\ +# 6.40 & 3.20 & 4.50 & 1.50 & versicolor \\ +# 6.90 & 3.10 & 4.90 & 1.50 & versicolor \\ +# 5.50 & 2.30 & 4.00 & 1.30 & versicolor \\ +# 6.50 & 2.80 & 4.60 & 1.50 & versicolor \\ +# 5.70 & 2.80 & 4.50 & 1.30 & versicolor \\ +# 6.30 & 3.30 & 4.70 & 1.60 & versicolor \\ +# 4.90 & 2.40 & 3.30 & 1.00 & versicolor \\ +# 6.60 & 2.90 & 4.60 & 1.30 & versicolor \\ +# 5.20 & 2.70 & 3.90 & 1.40 & versicolor \\ +# 5.00 & 2.00 & 3.50 & 1.00 & versicolor \\ +# 5.90 & 3.00 & 4.20 & 1.50 & versicolor \\ +# 6.00 & 2.20 & 4.00 & 1.00 & versicolor \\ +# 6.10 & 2.90 & 4.70 & 1.40 & versicolor \\ +# 5.60 & 2.90 & 3.60 & 1.30 & versicolor \\ +# 6.70 & 3.10 & 4.40 & 1.40 & versicolor \\ +# 5.60 & 3.00 & 4.50 & 1.50 & versicolor \\ +# 5.80 & 2.70 & 4.10 & 1.00 & versicolor \\ +# 6.20 & 2.20 & 4.50 & 1.50 & versicolor \\ +# 5.60 & 2.50 & 3.90 & 1.10 & versicolor \\ +# 5.90 & 3.20 & 4.80 & 1.80 & versicolor \\ +# 6.10 & 2.80 & 4.00 & 1.30 & versicolor \\ +# 6.30 & 2.50 & 4.90 & 1.50 & versicolor \\ +# 6.10 & 2.80 & 4.70 & 1.20 & versicolor \\ +# 6.40 & 2.90 & 4.30 & 1.30 & versicolor \\ +# 6.60 & 3.00 & 4.40 & 1.40 & versicolor \\ +# 6.80 & 2.80 & 4.80 & 1.40 & versicolor \\ +# 6.70 & 3.00 & 5.00 & 1.70 & versicolor \\ +# 6.00 & 2.90 & 4.50 & 1.50 & versicolor \\ +# 5.70 & 2.60 & 3.50 & 1.00 & versicolor \\ +# 5.50 & 2.40 & 3.80 & 1.10 & versicolor \\ +# 5.50 & 2.40 & 3.70 & 1.00 & versicolor \\ +# 5.80 & 2.70 & 3.90 & 1.20 & versicolor \\ +# 6.00 & 2.70 & 5.10 & 1.60 & versicolor \\ +# 5.40 & 3.00 & 4.50 & 1.50 & versicolor \\ +# 6.00 & 3.40 & 4.50 & 1.60 & versicolor \\ +# 6.70 & 3.10 & 4.70 & 1.50 & versicolor \\ +# 6.30 & 2.30 & 4.40 & 1.30 & versicolor \\ +# 5.60 & 3.00 & 4.10 & 1.30 & versicolor \\ +# 5.50 & 2.50 & 4.00 & 1.30 & versicolor \\ +# 5.50 & 2.60 & 4.40 & 1.20 & versicolor \\ +# 6.10 & 3.00 & 4.60 & 1.40 & versicolor \\ +# 5.80 & 2.60 & 4.00 & 1.20 & versicolor \\ +# 5.00 & 2.30 & 3.30 & 1.00 & versicolor \\ +# 5.60 & 2.70 & 4.20 & 1.30 & versicolor \\ +# 5.70 & 3.00 & 4.20 & 1.20 & versicolor \\ +# 5.70 & 2.90 & 4.20 & 1.30 & versicolor \\ +# 6.20 & 2.90 & 4.30 & 1.30 & versicolor \\ +# 5.10 & 2.50 & 3.00 & 1.10 & versicolor \\ +# 5.70 & 2.80 & 4.10 & 1.30 & versicolor \\ +# 6.30 & 3.30 & 6.00 & 2.50 & virginica \\ +# 5.80 & 2.70 & 5.10 & 1.90 & virginica \\ +# 7.10 & 3.00 & 5.90 & 2.10 & virginica \\ +# 6.30 & 2.90 & 5.60 & 1.80 & virginica \\ +# 6.50 & 3.00 & 5.80 & 2.20 & virginica \\ +# 7.60 & 3.00 & 6.60 & 2.10 & virginica \\ +# 4.90 & 2.50 & 4.50 & 1.70 & virginica \\ +# 7.30 & 2.90 & 6.30 & 1.80 & virginica \\ +# 6.70 & 2.50 & 5.80 & 1.80 & virginica \\ +# 7.20 & 3.60 & 6.10 & 2.50 & virginica \\ +# 6.50 & 3.20 & 5.10 & 2.00 & virginica \\ +# 6.40 & 2.70 & 5.30 & 1.90 & virginica \\ +# 6.80 & 3.00 & 5.50 & 2.10 & virginica \\ +# 5.70 & 2.50 & 5.00 & 2.00 & virginica \\ +# 5.80 & 2.80 & 5.10 & 2.40 & virginica \\ +# 6.40 & 3.20 & 5.30 & 2.30 & virginica \\ +# 6.50 & 3.00 & 5.50 & 1.80 & virginica \\ +# 7.70 & 3.80 & 6.70 & 2.20 & virginica \\ +# 7.70 & 2.60 & 6.90 & 2.30 & virginica \\ +# 6.00 & 2.20 & 5.00 & 1.50 & virginica \\ +# 6.90 & 3.20 & 5.70 & 2.30 & virginica \\ +# 5.60 & 2.80 & 4.90 & 2.00 & virginica \\ +# 7.70 & 2.80 & 6.70 & 2.00 & virginica \\ +# 6.30 & 2.70 & 4.90 & 1.80 & virginica \\ +# 6.70 & 3.30 & 5.70 & 2.10 & virginica \\ +# 7.20 & 3.20 & 6.00 & 1.80 & virginica \\ +# 6.20 & 2.80 & 4.80 & 1.80 & virginica \\ +# 6.10 & 3.00 & 4.90 & 1.80 & virginica \\ +# 6.40 & 2.80 & 5.60 & 2.10 & virginica \\ +# 7.20 & 3.00 & 5.80 & 1.60 & virginica \\ +# 7.40 & 2.80 & 6.10 & 1.90 & virginica \\ +# 7.90 & 3.80 & 6.40 & 2.00 & virginica \\ +# 6.40 & 2.80 & 5.60 & 2.20 & virginica \\ +# 6.30 & 2.80 & 5.10 & 1.50 & virginica \\ +# 6.10 & 2.60 & 5.60 & 1.40 & virginica \\ +# 7.70 & 3.00 & 6.10 & 2.30 & virginica \\ +# 6.30 & 3.40 & 5.60 & 2.40 & virginica \\ +# 6.40 & 3.10 & 5.50 & 1.80 & virginica \\ +# 6.00 & 3.00 & 4.80 & 1.80 & virginica \\ +# 6.90 & 3.10 & 5.40 & 2.10 & virginica \\ +# 6.70 & 3.10 & 5.60 & 2.40 & virginica \\ +# 6.90 & 3.10 & 5.10 & 2.30 & virginica \\ +# 5.80 & 2.70 & 5.10 & 1.90 & virginica \\ +# 6.80 & 3.20 & 5.90 & 2.30 & virginica \\ +# 6.70 & 3.30 & 5.70 & 2.50 & virginica \\ +# 6.70 & 3.00 & 5.20 & 2.30 & virginica \\ +# 6.30 & 2.50 & 5.00 & 1.90 & virginica \\ +# 6.50 & 3.00 & 5.20 & 2.00 & virginica \\ +# 6.20 & 3.40 & 5.40 & 2.30 & virginica \\ +# 5.90 & 3.00 & 5.10 & 1.80 & virginica \\ +# \hline +# \hline +# \end{longtable} \ No newline at end of file