From 66492997bbdc4f9766d7c4573e676fbdb9bd7def Mon Sep 17 00:00:00 2001 From: "Gergely Daroczi (@daroczig)" Date: Mon, 10 Sep 2018 01:04:43 +0200 Subject: [PATCH] fall back to slow R's table.expand --- R/pandoc.R | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/R/pandoc.R b/R/pandoc.R index 688f113b..0e66414a 100644 --- a/R/pandoc.R +++ b/R/pandoc.R @@ -639,7 +639,29 @@ pandoc.table.return <- function(t, caption, digits = panderOptions('digits'), de ## expands cells for output table.expand <- function(cells, cols.width, justify, sep.cols) { - .Call('pander_tableExpand_cpp', PACKAGE = 'pander', cells, cols.width, justify, sep.cols, style) + tryCatch( + .Call('pander_tableExpand_cpp', PACKAGE = 'pander', cells, cols.width, justify, sep.cols, style), + ## fall back to slow R function that works in case of C++ error + error = function(e) { + df <- data.frame(txt = cells, width = cols.width, justify = justify) + if (any(grepl('\n', df$txt))) { + if (style %in% c('simple', 'rmarkdown')) + stop('Pandoc does not support newlines in simple or Rmarkdown table format!') + res <- lapply(strsplit(as.character(df$txt), '\n'), unlist) + res.lines <- max(sapply(res, length)) + res <- paste( + sapply(1:res.lines, + function(i) table.expand(sapply(res, function(x) + ifelse(is.na(x[i]), ' ', x[i])), + cols.width, justify, sep.cols)), collapse = '\n') + return(res) + } else { + res <- apply(df, 1, function(x) + format(x[1], justify = x[3], + width = as.numeric(x[2]) + length(which(gregexpr("\\\\", x[1])[[1]] > 0)))) + return(paste0(sep.cols[1], paste(res, collapse = sep.cols[2]), sep.cols[3])) + } + }) } ## cell conversion to plain-ascii (deletion of markup characters)