Sweave xtable: how to position tables between text? - r

I have a number of tables with text around them describing them. Something like this:
This table shows blah blah...
<<echo=FALSE, results=tex>>=
print(
xtable(x,
caption = "blah", label = "tab:four", table.placement = "tbp", caption.placement = "top")
, size = "small", table.placement="ht")
#
This table shows blah blah...
<<echo=FALSE, results=tex>>=
print(
xtable(x,
caption = "blah", label = "tab:five", table.placement = "tbp", caption.placement = "top")
, size = "small", table.placement="ht")
#
I want all my descriptive text to be in line with the tables so that they follow in the sequence that I am writing them. But around the end of a page, some tables move off onto the next page and the descriptive text is just free floating. Is there some particular table.placement command that will ensure that everything stays the way that it is written?

The latex float package provides the float specifier H, which allows you to force tables and figures to be precisely in the location they occur in the latex code. For example:
\usepackage{float}
...
<<echo=FALSE, results=tex>>=
print(xtable(x),table.placement="H")
#

See here for the table placement. You could try "!h" to force the table to stay where you wan't.

I find \clearpage after a float is sometimes useful.

Related

Inline text in Markdown - adding results from code

I have been told that I can write a results section for a paper on Markdown. So say I want to get the value Robust_t_time_1 which I have derived in my R code section:
```{r fig.width=4, fig.height=4, echo=FALSE, message=FALSE,
warning=FALSE, include = TRUE}
coefs.robust <- data.frame(coef(summary(model_robust)))
t_values.robust1 <- coefs.robust$t.value
Robust_t_time_1 <- t_values.robust1[3]
Robust_t_time_1
```
And I am writing in the text section of Markdown and I want to quote this value. How do I refer to it? I have been trying t = {r} Robust_t_time_1 . This does not work. I know it should be possible.
To evaluate the value you have to use back ticks in the text: "t = `r Robust_t_time_1`"
You can also render it as an inline equation in the text by using dollar signs: "$t = `r Robust_t_time_1`$"

I have a for loop of qplots, how can you structure the plots individually using LaTeX code e.g adding captions to the plots

\begin{figure}[h]
\centering
<<echo=FALSE>>=
for(i in 2:38){
print(my_plot_function(i))
}
#
\end{figure}
This is my code, but what is happening is that when I compile my PDF I only get the first two plots that fit on the first page and I do not get the rest of the plots.
I would like to have all of the plots on separate pages.
And how would I go about adding captions to each individual plot in the for loop.
\documentclass{article}
\begin{document}
<<echo = FALSE, fig.cap = c("First, Second, Third"), results = "asis">>=
library(ggplot2)
for (i in 1:3) {
print(qplot(x = 1, y = i))
cat("Arbitrary \\LaTeX code! \\clearpage")
}
#
\end{document}
You can use the chunk option fig.cap to add captions to your plots. Note that this will wrap your figure in a figure environment, turning it into a float.
Use the chunk option results="asis" to be able to print arbitrary text (including LaTeX markup) into your document using cat().

RMarkdown: Long text in code chunks with multiple graphics

I am preparing a R markdown document to plot some graphics and some short text accompanying each graphic. The graphics are generated in a code chunk within a loop.
Right now, it looks like this:
First, all the text is outputted, then the graphics. I would like this pattern:
text
graphic
text
graphic
text
graphic
Also, I don't know how to apply line breaks in the cat() output. This is needed to make the whole label visible.
Any ideas on these problems?
This is my code:
```{r, echo = FALSE, comment='', fig.width=8, fig.height=3, fig.show='hold', fig.align='center'}
# vars is a data.frame with some variables names
for (i in 1:nrow(vars)){
options(warn=-1)
label <- 'some long text'
cat(label)
capture.output( data <- analysisFunction(name) )
capture.output( plottingFunctionWithGGplot2(data) )
cat('\n\n')
}
```
Thanks.

R Markdown - xtable with longtable and scalebox outputs scalebox value

I am creating a PDF document with with rmarkdown and knitr. Below is an example code chunk. When knitting to PDF it prints the scalebox value to the PDF, which I don't want. My actual table is much wider so using the scalebox argument is necessary.
```{r, results = 'asis', echo = FALSE, message = FALSE, warning=FALSE}
x <- matrix(rnorm(1000), ncol = 10)
x.big <- xtable(x)
print.xtable(x.big, hline.after=c(-1), tabular.environment = "longtable", scalebox = 0.7)
```
This only happens when using the longtable tabular environment. Running the same code chunk with the standard tabular environment doesn't output the scalebox info. I've tried setting every comment argument in the print.xtable function and the r code chunk to FALSE but with no luck.
How can I output my PDF file without that scalebox text being printed?
I haven't found a way to get around the scalebox issue. What I ended up doing was using was the size argument in print.xtable instead. Below is a sample function where size is an integer representing the desired size of the font.
outputXtableTest <- function( df, size){
sizeNew = paste0("\\fontsize{", size,"pt}{", size+1, "pt}\\selectfont")
print.xtable(
df, hline.after=c(-1,0, 1:nrow(table)),
tabular.environment = 'longtable',
floating = FALSE, size = sizeNew
)
}
See this post for more information.

How to place xtable object to the left side of page

Question: How to place xtable object to the left side of page or how to disable centering globally.
I'm struggling to figure out how to place xtable object on the left side. I have got a *.Rmd file and all this goes to the relevant r chunk.
require(xtable)
df <- data.frame(x=seq(1,10,1),y=rnorm(10))
Xtab <- xtable(df, digits=0, caption="\\textbf{MINIMAL/IDEAL}",
floating=FALSE, latex.environments = c("left"))
print(Xtab, size = "small", include.colnames=FALSE)
I have included the following after reading various sources (print.xtable; xtable manual etc.)
1) floating=FALSE
2) latex.environments = c("left")
I have searched SO and used some of the hints but all failed.
It seems to make quite a difference whether you pass a parameter to xtable() or to print(xtable()). The following chunk creates a table according to your data that is aligned at the left of the page in the pdf file.
```{r, results='asis',echo=FALSE}
library(xtable)
df <- data.frame(x=seq(1, 10, 1),y = rnorm(10))
print(xtable(df,digits=0, caption="\\textbf{MINIMAL/IDEAL}"), include.colnames=FALSE, size = "small", comment=FALSE, latex.environments="flushleft")
```
However, as you can see, the caption remains at the center of the page.

Resources