2

I have the following piece of R- / / minipage combo code.

What I am trying to do is get the xtable output to display within the minipage, along with other text paragraphs surrounding it. While my second xtable displays fine with markdown, the first one just repeats the code segment.

Any idea how I can get it to work?

---
output:
  pdf_document: default
header-includes:
- \usepackage[dvipsnames]{xcolor}
- \usepackage{ragged2e}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{\textsc{blah blah}}
- \rhead{\textsc{\bfseries \large blah blah}}
- \renewcommand{\footrulewidth}{0.4pt}
- \lfoot{blah blah}
- \fancypagestyle{plain}{\pagestyle{fancy}}
---
\fcolorbox{Fuchsia}{Fuchsia}{\begin{minipage}{46.5em}\centering\textbf{\large\color{white}ABOUT THIS TEST}\end{minipage}}
\offinterlineskip
\fbox{\begin{minipage}[t]{46.5em}
\vspace*{0.3cm}\centering\textbf{METHODOLOGY}\vspace*{0.3cm}

\justify blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah \vspace*{0.3cm}

\justify blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah \vspace*{0.3cm}

<<echo=FALSE, results=tex>>=
library(xtable) 
print(xtable(head(mtcars)), floating = FALSE, include.rownames = FALSE) 
@ 
\end{minipage}}

\bigskip

**Table 1.** Blah blah blah Blah blah blah Blah blah blah Blah blah blah Blah

```{r, xtable, results = 'asis', echo=FALSE, eval=TRUE, message=FALSE}
library(xtable)
x <- head(mtcars)
print(
  xtable(x, caption = 'Blah Blah'), caption.placement = 'top',
  include.colnames = FALSE, comment = FALSE
  )
```
  • Check if results=tex is a valid option for markdown. Isn't it used with Sweave for making tables? results='asis' is correct, as you have already shown. – Ross Dec 22 '18 at 10:30
3
+25

You cannot use result=tex option of Sweave in a R chunk in R markdown with uses the knitr syntax, but more important, you cannot use the R chunk syntax for noweb (.Rnw) files. Thas is, some like:

<<name,results='asis', option, option, ...>>= 
2+2 
@

Is still wrong, even if the chunk is inside the inside a LaTeX environment. Instead you must use always the R markdown syntax:

```{r name, results='asis', option, option, ...}
2+2
```  

In the MWE below there are many others changes in LaTeX, R and markdown irrelevants for the question, to simplify the example (removing headers,etc.) and the code (tcolorboxinstead of a minipage,etc.) or style suggestions (booktabsfor table rules,etc.}. The result:

mwe

---
header-includes:
- \usepackage[most]{tcolorbox}
- \usepackage{caption}
- \usepackage{parskip}
- \usepackage{booktabs}
output:
  pdf_document: default
---

\begin{tcolorbox}[colbacktitle=blue!85!red!60, 
fonttitle=\bfseries\sffamily\centering, 
colback=green!05,boxsep=1mm,arc=0mm,boxrule=1pt, 
title=ABOUT THIS TEST]\parskip1em

\subsection*{\centering METHODOLOGY}

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah blah blah blah.

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah blah blah blah.

\centering
\captionof{table}{The head of mtcars}

```{r table, echo=F,results="asis"}
library(xtable) 
x <- head(mtcars)
print(xtable(x, digits=c(0,1,0,0,0,2,2,2,0,0,0,0),align="lccccccccccc"),
floating = F, include.rownames = T,booktabs=T) 
```
\end{tcolorbox}

```{r xtable, results = 'asis', echo=F}
print(xtable(x, caption="Blah blah Blah blah blah Blah blah blah."),
caption.placement = 'top',  include.colnames = F, comment = F )
```

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.