0

I'm working on iteratively producing LaTeX tables using knitr. All is well except I'm left with extra markup before each table. Here's a simple example, though this would ideally work as a template for more complex problems, ie different-size tables, varying data sets etc.

What can I do to get rid of the extra text before each table?

\documentclass{article}
\usepackage{setspace, relsize}
\usepackage[margin=.5in, landscape]{geometry} 
\usepackage{pdfpages}
\begin{document}

<<setup, include=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE, cache = FALSE, error = FALSE)
library("ggplot2")
library("knitr")
library("Hmisc")

mytable_function = function(mydata){
  foo = as.matrix(head(mydata))
  colnames(foo) = names(mydata)
  rownames(foo) = c("First", "Second", "Third", "Fourth", "Fifth", "Sixth")
  return(foo)
}

template <- "<<thisthing-{{i}}>>=
mytable = mytable_function(iris[iris$Species == unique(iris$Species)[i],])
latex(mytable, file = '',
        title = '',
        where = '!h',
        caption = 'This is a table',
        col.just = rep('r', ncol(mytable)))
@"

 for(i in 1:3){
    cat(knit(text = knit_expand(text = template, i = i, quiet = TRUE)))
 }

@

\end{document}

enter image description here

Fwiw here's a similar question I asked a while ago but because I'm producing tables here and not figures I think this is a slightly different solution. Print a list of dynamically-sized plots in knitr

Community
  • 1
  • 1
Nancy
  • 3,989
  • 5
  • 31
  • 49
  • If you are going to be using these tables in another document, you might want to just save the text output of each and then use `\input` (I think that the right Latex command) to read them in your final document. One example is using `xtable`, create the `xtable` object, then in `print()` you can add a file argument to save the output. I suspect that the `Hmisc` function has a similar argument. – lmo Sep 19 '16 at 19:19

1 Answers1

1

The provided code does not match the output you presented. Actually, it produces no output whatsoever.

Step 0: Reproduce output from the question

  • include=FALSE on the only chunk in the document is quite fatal … replace by echo=FALSE.
  • The main chunk (setup) as well as the template chunk need results="asis".
  • message=FALSE should be a chunk option of setup. Setting it as default options within setup won't affect messages from the current chunk.

Step 1: Immediate issue

This line

cat(knit(text = knit_expand(text = template, i = i, quiet = TRUE)))

shoud be

cat(knit(text = knit_expand(text = template, i = i), quiet = TRUE))

quiet is an argument of knit, not knit_expand.

Step 2: A better solution

Although this works, it's an overly complicated overkill. The answer you linked to dynamically generated chunks because fig.height is not vectorized the way it would be required for that case. Here, we can just use a single chunk:

\documentclass{article}
\begin{document}

<<setup, echo = FALSE, results='asis', message = FALSE>>=

mytable_function = function(mydata){
  foo = as.matrix(head(mydata))
  colnames(foo) = names(mydata)
  rownames(foo) = c("First", "Second", "Third", "Fourth", "Fifth", "Sixth")
  return(foo)
}

for(i in 1:3){
  mytable = mytable_function(iris[iris$Species == unique(iris$Species)[i],])
  Hmisc::latex(mytable, 
        file = '',
        title = '',
        where = '!h',
        caption = 'This is a table',
        col.just = rep('r', ncol(mytable)))
}

@
\end{document}
CL.
  • 14,577
  • 5
  • 46
  • 73