1

I cannot understand why the chunks in rmarkdown would place tables at the end of the document. If I allow ECHO = TRUE in the chunk header, the code gets output in the correct place, but the table (which contains wrapped long lines) gets placed at the document end.

```{r what_Modality, echo=FALSE, results='asis'}

df.Modality <- as.data.frame(head(na.omit(data_Europe_clean$q_0006),12))
colnames(df.Modality) <- c("Experience in modality")
xt.Modality <- xtable(df.Modality, caption = "What modality")
align(xt.Modality) <- c('l', 'p{3.5in}')
print(xt.Modality)

```

This happens also if I include fewer rows (4) in the table.

I added a picture of how the table looks on the last page vertically centered. The YAML header is simple; I included it too for completeness sake. I am working in Rstudio on Linux, using knitr and XeLaTex for rendering.

---
title: "Psychotherapy Study"
author: "RKTG"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: rmarkdown::tufte_handout
---

I have added also relevant excerpts from the intermediate .tex file. Firstly there is the whole header - everything just before \begin{document}:

\documentclass[]{tufte-handout}

% ams
\usepackage{amssymb,amsmath}

\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \makeatletter
  \@ifpackageloaded{fontspec}{}{\usepackage{fontspec}}
  \makeatother
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
  \makeatletter
  \@ifpackageloaded{soul}{
     \renewcommand\allcapsspacing[1]{{\addfontfeature{LetterSpace=15}#1}}
     \renewcommand\smallcapsspacing[1]{{\addfontfeature{LetterSpace=10}#1}}
   }{}
  \makeatother
\fi

% graphix
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}

% booktabs
\usepackage{booktabs}

% url
\usepackage{url}

% hyperref
\usepackage{hyperref}

% units.
\usepackage{units}


\setcounter{secnumdepth}{-1}

% citations

% pandoc syntax highlighting

% longtable

% multiplecol
\usepackage{multicol}

% strikeout
\usepackage[normalem]{ulem}

% morefloats
\usepackage{morefloats}


% tightlist macro required by pandoc >= 1.14
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% title / author / date
\title{Psychotherapy Study}
\author{RKTG}
\date{02 April, 2016}

\begin{document}

and this is a section containing the table. The text stays in its correct place, but the table travels to the very end of the document.

\section{What psychotherapy modality have you trained
in?}\label{what-psychotherapy-modality-have-you-trained-in}

This was an optional question presented to only those 547 respondents
who had answered positively to the previous question i.e.~those who had
trained in any modality of psychotherapy. We wanted to get a qualitative
overview as to what therapies they had trained in. Since this number is
quite large we can only present you with a sample summary of these 329
answers, but to give a feel for what they were like we have included the
first 12 rows of these answers.

\begin{table}[ht]
\centering
\begin{tabular}{lp{3.5in}}
  \toprule
 & Experience in modality \\ 
  \midrule
1 & psychoanalysis \\ 
  2 & Cognitieve Behavioural Therapy \\ 
  3 & We received a introduction course in cliented centered en systemic therapy yet. \\ 
  4 & Family and Marital Therapy \\ 
   \bottomrule
\end{tabular}
\caption{What modality have trainees had experience with} 
\end{table}

\subsection{Summary of psychotherapeutic methods that trainees had
experience
in}\label{summary-of-psychotherapeutic-methods-that-trainees-had-experience-in}

We summarised data along the following categories after reading them
manually. Lorem ipsum dolor sit amet, dis quis nascetur felis curabitur
tristique. Arcu nisi elementum in in at. Volutpat lobortis vulputate in
sed montes vel malesuada, felis sapien. Arcu mauris vestibulum aenean
placerat donec sit dictum odio conubia ante in nisi. Suspendisse,
pharetra euismod tempor rhoncus ac curae urna non. Lorem eleifend dolor
nascetur ex! Sit, in augue habitant ultrices per netus sit, primis vel.
Nisl sed fermentum ultricies sapien montes amet magna in cubilia.,
Phasellus eget et erat mi sem ac, non. Rhoncus est in posuere
pellentesque, class lobortis. Faucibus turpis, taciti vitae fames. 

Right after this a new section starts. And lastly I have included the screenshot of the table on the last page:

Screenshot of the xtable output

  • 1
    Welcomet to TeX - LaTeX! I am not familiar with rmarkdown, but is there an intermediate .tex file produced? If so what is the markup of the table environment in that file? – Andrew Swann Apr 1 '16 at 8:48
  • 1
    the whole point about a latex table environment is that it is a floating environment that may be moved to help page breaking. If nowhere feasible is found it will appear at the end. Without seeing more of the generated latex can not say why that should be the case here. – David Carlisle Apr 1 '16 at 8:49
  • Thanks, this is very helpful and is pointing me in the right direction. I will save intermediate .tex files and paste the relevant bits here (if I can find them). I am a bit worried it might be long and I'd have to refer to pastebin for the whole output. – r0berts Apr 1 '16 at 12:02
  • I added information from the .tex file. It looks simpler than I imagined, but I still cannot figure out why the table is placed at the very end. – r0berts Apr 2 '16 at 9:42
  • Thanks very much for your input - I solved my problem and have submitted the answer. I reckon that extra information from .tex file was useful for me to get my head around what is going on, but does not substantially add to the picture in this case. So maybe I should remove it from my question. – r0berts Apr 2 '16 at 14:12
2

After some helpful pointers I examined latex code and looked at several different options to work with tables in R-Markdown. I tried using pander package, but the results were not as straightforward as with xtable, and I like xtable producing straight latex output - there is no intermediate markdown step, which I think there is with pander. But pander would be a blessing where I would not be converting to PDF.

so all one has to do is to pass appropriate option to print(xtable(your.data.frame)). print in this case comes from package xtable and is fully known as print.xtable - which you can examine in ? help. The parameter that needs to be added is table.placement. So the code that has slight amendment works beautifully:

```{r what_Modality, echo=FALSE, results='asis'}

df.Modality <- as.data.frame(head(na.omit(data_Europe_clean$q_0006),12))
colnames(df.Modality) <- c("Experience in modality")
xt.Modality <- xtable(df.Modality, caption = "What modality")
align(xt.Modality) <- c('l', 'p{3.5in}')
print(xt.Modality, table.placement="!h")

```

Note: it does seem that using echo=FALSE in the chunk header is important - if you display the R code, then table may skip further down the paragraph order despite !h

! - overrides any options h - means here - so that the table stays where it is declared. Further information can be found at wikibooks. Oh yes - in case one wonders about aligning the xtable object in the line before last - it is necessary to achieve long line wrapping, which I have in this one-column table displaying survey textual responses output.

  • You can also use floating = FALSE in print.xtable. – alistaire Dec 9 '16 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.