1

I'm using the knitr package to produce some basic table. I'm displaying the first n rows of a table on one slide and the last n rows of the same table on the next slide. I'm using head() and tail() to get the first and last rows.

Using head() produces what I expect:

library(dplyr)
library(pander)

data.frame(a = sample(LETTERS, 10), 
           b = sample(LETTERS, 10)) %>%
head(5) %>% 
pander()   

Produces

-------
 a   b 
--- ---
 G   B 

 I   P 

 N   H 

 U   W 

 V   A 
-------

but

data.frame(a = sample(LETTERS, 10), 
           b = sample(LETTERS, 10)) %>%
tail(5) %>% 
pander()

produces

----------------
     a   b 
-------- --- ---
 **6**    Y   B 

 **7**    F   O 

 **8**    B   H 

 **9**    R   Y 

 **10**   W   X 
----------------

and I can't remove the row names from the final table.

The top answer here suggests that Pander removes the row names from a table when they are 1:n but here, they begin with 1, so they're not removed automatically.

How can I prevent the row numbers from being displayed? I've tried using select() as well as using the UPDATE from this question to no avail.

Steven
  • 3,238
  • 21
  • 50

2 Answers2

3

If you don't want the rownames to be displayed you could remove them using rownames(df) <- NULL (after the tail) but I don't know how to use this in the pipe. One hack would be to convert to a tibble and then convert back to a data frame (because tibbles don't store rownames but you can't use pander on tibbles apparently):

data.frame(a = sample(LETTERS, 10), 
           b = sample(LETTERS, 10)) %>%
  dplyr::as.tbl() %>%
  tail(5) %>% 
  as.data.frame() %>%
  pander()
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • Yeah, I couldn't figure out how to use `rownames(df) <- NULL` with the pipe operator either. Including `as.tbl()` and `as.data.frame()` works for now. There's gotta be a better way, though. – Steven Jun 30 '17 at 14:18
1

You probably want to open an issue on the github account of pander

Another hack is using slice:

library(dplyr)
library(pander)

data.frame(a = sample(LETTERS, 10), 
           b = sample(LETTERS, 10)) %>%
  slice(n()-5:n()) %>% 
  pander()
Hanjo Odendaal
  • 1,395
  • 2
  • 13
  • 32