rr-markdownknitrxtable

xTable Knitr Error - every line is printed underneath in PDF


I try to knitr my R-Markdown as PDF and want to print some tables using xtable, but unfortunately I print every item of my table underneath: Print Output (FYI: I'm in college and asked to use xtable for this exercise).

I use the following code (there are more tables but they follow the same pattern; the library xtable is already loaded in my R-Markdown earlier):

```{r echo=FALSE}
swisstable <- sapply(swiss,function(x) {
c(Mean = mean(x), SD = sd(x), Median = median(x), IQR = IQR(x))
} )
```

```{r echo=FALSE}
swisstable <- xtable(swisstable,align=xalign(swisstable),digits=xdigits(swisstable), 
display=xdisplay(swisstable))
```

```{r results='asis', echo=FALSE}
print(xtable(swisstable, digits = 1, caption = "Tabelle 2: Überblick über den Datensatz 
swiss"),type='html')
```

I'm grateful for any help, thanks, Martina


Solution

  • If you want to knit in a pdf keep results='asis' and don't print xtable.

    ```{r echo=FALSE}
    library(xtable)
    swisstable <- sapply(swiss,function(x) {
       c(Mean = mean(x), SD = sd(x), Median = median(x), IQR = IQR(x))
     })
    ```
    
    ```{r results='asis', echo=FALSE}
      xtable(swisstable, digits = 1, caption = "Tabelle 2: Überblick über den Datensatz swiss")
    ```
    

    enter image description here