From 7ff51d89fce8bb7c98359115adc1ea47898ddbba Mon Sep 17 00:00:00 2001 From: Barbara Borges Ribeiro Date: Mon, 7 Mar 2016 12:21:39 +0000 Subject: [PATCH] check if rownames are numbers or strings --- R/render-bootstrap-table.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/R/render-bootstrap-table.R b/R/render-bootstrap-table.R index 263d50572..53182ed11 100644 --- a/R/render-bootstrap-table.R +++ b/R/render-bootstrap-table.R @@ -67,7 +67,26 @@ renderBootstrapTable <- function(expr, format="basic", width="auto", non_xtable_args <- dots[setdiff(names(dots), xtable_argnames)] # Figure out column alignment - if ( is.null(align) ) xtable_args <- c( xtable_args, align = NULL ) + ## Case 1: if align=NULL, check if rownames are numbers. If not, make + ## sure to left align them (xtable right aligns them by default, which + ## looks weird when the rownames are strings). + if ( is.null(align) ){ + n <- rownames( data ) + if ( !( suppressWarnings( is.na( all( n == as.character( as.numeric(n) )))))){ + xtable_args <- c( xtable_args, align = NULL ) + } + else { + cols <- "l" + for ( i in 1:ncol(data) ){ + cls <- class( data[,i] ) + if ( cls=="numeric" || cls=="integer" ) cols <- paste0( cols, "r" ) + else cols <- paste0( cols, "l" ) + } + xtable_args <- c( xtable_args, align = cols ) + } + } + ## Case 2: if align!=NULL, check if it is only one character or a vector + ## and process it accordingly. else { num_cols <- ifelse( rownames, nchar(align), nchar(align) + 1 ) valid <- !grepl( "[^lcr]", align )