Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you print table in knitr

Tags:

r

knitr

I am trying to us knitr to print data frame in table format using xtable:

```{r xtable,fig.width=10, fig.height=8, message=FALSE, results = 'asis', echo=FALSE, warning=FALSE, fig.cap='long caption', fig.scap='short',tidy=FALSE}

print(xtable(d),format="markdown")
```

This is the data frame d:

d <- structure(list(Hostname = structure(c(8L, 8L, 9L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L), .Label = c("db01", "db02", "farm01", "farm02", 
"tom01", "tom02", "tom03", "web01", "web03"), class = "factor"), 
    Date = structure(c(6L, 10L, 5L, 3L, 2L, 1L, 8L, 9L, 7L, 4L
    ), .Label = c("10/5/2015 1:15", "10/5/2015 1:30", "10/5/2015 2:15", 
    "10/5/2015 4:30", "10/5/2015 8:30", "10/5/2015 8:45", "10/6/2015 8:15", 
    "10/6/2015 8:30", "9/11/2015 5:00", "9/11/2015 6:00"), class = "factor"), 
    Cpubusy = c(31L, 20L, 30L, 20L, 18L, 20L, 41L, 21L, 29L, 
    24L), UsedPercentMemory = c(99L, 98L, 95L, 99L, 99L, 99L, 
    99L, 98L, 63L, 99L)), .Names = c("Hostname", "Date", "Cpubusy", 
"UsedPercentMemory"), class = "data.frame", row.names = c(NA, 
-10L))

Any ideas what I am missing here?

like image 328
user1471980 Avatar asked Oct 28 '15 16:10

user1471980


People also ask

Can you make a table in R Markdown?

By default, R Markdown displays data frames and matrixes as they would be in the R terminal (in a monospaced font). If you prefer that data be displayed with additional formatting you can use the knitr::kable function, as in the . Rmd file below.

How do I insert a table in R Markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

How do I show all rows in R Markdown?

options(tibble. width = Inf) # displays all columns. options(tibble. print_max = Inf) # to show all the rows.


2 Answers

Try kable from knitr. It will format the table nicely.

enter image description here

If you would like to use xtable try:

print(xtable(d), type="latex", comment=FALSE)
like image 153
Pierre L Avatar answered Oct 18 '22 04:10

Pierre L


While Pierre’s solution works, this should ideally happen automatically. Luckily, you can use knitr hooks to make this work.

That is, given this code:

```{r}
d
```

We want knitr to automatically produce a nicely formatted table, without having to invoke a formatting function manually.

Here’s some code I’m using for that. You need to put this at the beginning of your knitr document, or in the code that’s compiling your document:

opts_chunk$set(render = function (object, ...) {
    if (pander_supported(object))
        pander(object, style = 'rmarkdown')
    else if (isS4(object))
        show(object)
    else
        print(object)
})

This uses pander and additionally requires a helper function, pander_supported:

library(pander)

pander_supported = function (object)
    UseMethod('pander_supported')

pander_supported.default = function (object)
    any(class(object) %in% sub('^pander\\.', '', methods('pander')))

pander.table = function (x, ...)
    pander(`rownames<-`(rbind(x), NULL), ...)

For nicer formatting, I also use these defaults:

panderOptions('table.split.table', Inf)
panderOptions('table.alignment.default',
              function (df) ifelse(sapply(df, is.numeric), 'right', 'left'))
panderOptions('table.alignment.rownames', 'left')
like image 45
Konrad Rudolph Avatar answered Oct 18 '22 05:10

Konrad Rudolph