Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pander.tabular again #158

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install:
- "[ ! -d ~/R ] && mkdir ~/R"
- R --version
- R -e '.libPaths(); sessionInfo()'
- Rscript -e 'install.packages(c("ggplot2", "testthat", "koRpus", "descr", "Rcpp", "microbenchmark", "pander", "devtools", "XML"), dep = TRUE, repos = "http://cran.r-project.org")'
- Rscript -e 'install.packages(c("ggplot2", "testthat", "koRpus", "descr", "Rcpp", "microbenchmark", "pander", "devtools", "XML", "tables"), dep = TRUE, repos = "http://cran.r-project.org")'
- Rscript -e 'devtools::install_github("jimhester/covr")'

# run tests
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Suggests:
nlme,
descr,
MASS,
knitr
knitr,
tables
SystemRequirements: pandoc (http://johnmacfarlane.net/pandoc) for exporting
markdown files to other formats.
LinkingTo: Rcpp
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ S3method(pander,summary.prcomp)
S3method(pander,survdiff)
S3method(pander,survfit)
S3method(pander,table)
S3method(pander,tabular)
S3method(pander,ts)
S3method(pander,zoo)
export(Pandoc)
Expand Down
28 changes: 28 additions & 0 deletions R/S3.R
Original file line number Diff line number Diff line change
Expand Up @@ -1504,3 +1504,31 @@ pander.function <- function(x, add.name = FALSE, verbatim = TRUE, syntax.highlig
cat('```')

}

#' Pander method for tabular class
#'
#' @param x an tabular object
#' @param ... ignored parameters
#' @export
pander.tabular <- function(x, ...) {
# Get the cols and rows header
colLabels <- attr(x, 'colLabels')
rowLabels <- attr(x, 'rowLabels')
content <- format(x, ...)

# Replace NA values with empty strings
colLabels[is.na(colLabels)] <- ''
rowLabels[is.na(rowLabels)] <- ''

# Create an empty matrix to get the same size
header <- matrix(data="", nrow = nrow(colLabels), ncol = ncol(rowLabels))
header[nrow(colLabels), 1:ncol(rowLabels)] <- colnames(rowLabels)
# Add the row labels to the table header
header <- cbind(header, colLabels)

colnames(rowLabels) <- NULL
table <- cbind(rowLabels, content)
colnames(table) <- apply(header, 2, paste, collapse= '\\ \n')

pandoc.table(table, keep.line.breaks = TRUE, ...)
}
20 changes: 20 additions & 0 deletions inst/tests/test-S3.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,23 @@ test_that('table.expand behaves correctly',{
# unicode string issue
expect_equal(pandoc.table.return(data.frame(a = 'ßß')), "\n---\n a \n---\nßß \n---\n\n")
})

context("pander.tabular")
test_that('tables::tabular behaves correctly', {
if(!suppressMessages(require('tables'))) {
skip('Package tables not installed : skipping pander.tabular tests')
}
# checking nested columns
x <- tabular((Species + 1) ~ (n=1) + Format(digits=2) * (Sepal.Length + Sepal.Width)*(mean + sd), data=iris)
tabularmd <- pander.return(x);

expect_equal(tabularmd[3], " \\ \\ Sepal.Length\\ \\ Sepal.Width\\ \\ ")
expect_equal(tabularmd[4], " Species n mean sd mean sd ")

# checking nested rows
x <- tabular(as.factor(am) * (mean+median) ~ (mpg+hp+qsec), data = mtcars)
tabularmd <- pander.return(x);

expect_equal(tabularmd[5], " 0 mean 17.15 160.3 18.18 ")
expect_equal(tabularmd[7], " median 17.30 175.0 17.82 ")
})
17 changes: 17 additions & 0 deletions man/pander.tabular.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/S3.R
\name{pander.tabular}
\alias{pander.tabular}
\title{Pander method for tabular class}
\usage{
\method{pander}{tabular}(x, ...)
}
\arguments{
\item{x}{an tabular object}

\item{...}{ignored parameters}
}
\description{
Pander method for tabular class
}