More than three xtables on a page in knitr

Tags: r knitr xtable
Question!

I am trying to place more than three tables on one page of a pdf using knitr and xtable. The following code places the fourth table on the second page. If they are size appropriate, is there a way to include all of the tables on one page?

The following example places the fourth table on the second page, although there appears to be room on the first page.

---
title: "Example Code"
---

```{r setup, include=FALSE}    
knitr::opts_chunk$set(echo = TRUE)

options(xtable.comment = FALSE)

library(xtable)    
```

```{r t1, results='asis'}
xtable(cars[1:2,])
```

```{r t2, results='asis'}
xtable(cars[3:4,])
```

```{r t3, results='asis'}
xtable(cars[5:6,])
```

```{r t4, results='asis'}
xtable(cars[7:8,])
```


Answers

I think this is due to one of the settings like totalnumber or \textfraction explained here on TEX.SE. However, I couldn't come up with settings that place all four tables on one page so far.

Therefore, if the tables don't need to actually float, I suggest loading the float package and using the position specifier H:

---
title: "Example Code"
output: pdf_document
header-includes:
  - \usepackage{float}
---


```{r setup, include=FALSE}
options(xtable.table.placement  = "H")
library(xtable)
```

```{r t1, results='asis'}
xtable(cars[1:2,])
```

```{r t2, results='asis'}
xtable(cars[3:4,])
```

```{r t3, results='asis'}
xtable(cars[5:6,])
```

```{r t4, results='asis'}
xtable(cars[7:8,])
```

Output

By : CL.


Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.

You could try something like the following:

$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
    $arr[] = $var; //add the var to the array 
}

print_r($arr);

Array
(
    [0] => miniTrack
    [1] => boxtrack
)

And if you need to create a variable for each item, you can create "variable variables":

foreach($vars as $var) {
    $my_var = $$var; //variable variable
}


You can convert your strings to array like this. It may be not ideal but you can use try this.

$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);

Here you can iterate your array whatever you want.



This video can help you solving your question :)
By: admin