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

Allow for column alignment for org table export #2391

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,19 @@ kable_jira = function(x, caption = NULL, padding = 1, ...) {
}

# Emacs Org-mode table
kable_org = function(...) {
res = kable_pipe(..., caption.label = '#+CAPTION:')
i = grep('^[-:|]+$', res) # find the line like |--:|---| under header
if (length(i)) {
i = i[1]
res[i] = gsub('(-|:)[|](-|:)', '\\1+\\2', res[i]) # use + as separator
}
res
kable_org = function(x, caption = NULL, padding = 1, caption.label = '#+CAPTION:', ...) {
if (length(x)==0) return("")
is_null_colnames = is.null(colnames(x))
if (is_null_colnames) colnames(x) = rep('', ncol(x))
res = kable_mark(x, c(NA, "-", NA), '|', padding, align.fun = function(s, a) {
if (is.null(a)) return(s)
r = c(l = '<l>', c = '<c>', r = '<r>')
rbind(r[a], rep("---",length(a)))
}, ...)
res = sprintf('|%s|', res)
res = gsub('[-][-][|][-]', '--+-', res)
if (is_null_colnames) res = res[-c(1,3)]
kable_pandoc_caption(res, caption, caption.label)
}

kable_pandoc_caption = function(x, caption, label = 'Table:') {
Expand Down
3 changes: 2 additions & 1 deletion tests/testit/test-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ assert('kable() works with the jira format', {
})

assert('kable() works with the org format', {
(kable2(m, 'org') %==% c('| | x| y|', '|:--+--:+--:|', '|a | 1| 2|'))
(kable2(m, 'org') %==% c('| | x| y|', '|<l>|<r>|<r>|', '|---+---+---|',
'|a | 1| 2|'))
})

assert('kable() does not add extra spaces to character columns', {
Expand Down
Loading