0

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,131
  • 17
  • 47

2 Answers2

2

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é
  • 10,953
  • 2
  • 24
  • 72
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,284
  • 1
  • 13
  • 27