Skip to content

write.ctable doesnt' play nicely with hooks for numbering and referencing tables  #11

@nutterb

Description

@nutterb

write.ctable doesnt' play nicely with hooks for numbering and referencing tables


title: "Auto-numbering and cross-referencing of figures and tables in rmarkdown"

output: html_document

# This is a knitr hook to enable figure and table captions
# number figures
# from http://stackoverflow.com/a/18672268/1036500
library("knitr")
tn = local({
  i = 0
  function(x) {
    i <<- i + 1
    paste("<table><caption>", 'Figure ', i, ': ', x, "</caption><colgroup><col width='100'></col></colgroup><thead><tr class='header'></tr></thead><tbody></tbody></table><p>", sep = '')
  }
})
knit_hooks$set(fig.cap = function(before, options, envir) {
  if(!before)
    tn(options$fig.cap)
})
default_output_hook = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  if (is.null(options$fig.cap) == F)  
    x
  else
    default_output_hook(x,options)
})

# number tables
# from http://stackoverflow.com/a/18672268/1036500
to = local({
  i = 0
  function(x) {
    i <<- i + 1
    paste("<table><caption>", 'Table ', i, ': ', x, "</caption><colgroup><col width='100'></col></colgroup><thead><tr class='header'></tr></thead><tbody></tbody></table><p>", sep = '')
  }
})
knit_hooks$set(tab.cap = function(before, options, envir) {
  if(!before)
    to(options$tab.cap)
})
default_output_hook = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  if (is.null(options$tab.cap) == F)  
    x
  else
    default_output_hook(x,options)
})
library(knitr)
library(kfigr) # devtools::install_github("github mkoohafkan/kfigr")
opts_chunk$set(message=FALSE, warning=FALSE)
library(lazyWeave)
library(stringr)
options(lazyReportFormat = "markdown")

summaries

Using the knitr option "echo=F" seems to cause the problem.

data(mtcars)

mtcars$group = sample(c("Bad", "Good"), size=nrow(mtcars), replace=T)

tab1 = catconttable(data = mtcars,
                    vars = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"),
                    normal = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"),
                    byVar = "group")
tab = write.ctable(tab1)
tab = str_split(tab, "\n")[[1]]
tab = tab[c(6,4,5,7:length(tab))]
tab = paste(tab, collapse="\n")
cat(tab)

See r figr("ATable", T,F) for summaries of the variables in the cars dataset.

data(mtcars)

mtcars$group = sample(c("Bad", "Good"), size=nrow(mtcars), replace=T)

tab1 = catconttable(data = mtcars,
                    vars = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"),
                    normal = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"),
                    byVar = "group")
tab = write.ctable(tab1)
tab = str_split(tab, "\n")[[1]]
tab = tab[c(6,4,5,7:length(tab))]
tab = paste(tab, collapse="\n")
cat(tab)

See r figr("ATable2", T,F) for summaries of the variables in the cars dataset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions