How do I remove row names when using pander to brew a report?

随声附和 提交于 2020-01-02 07:49:20

问题


I'm trying to get my head around using R to generate reports and think I have settled on trying to just use pander, after confusing myself with various combinations of knitr, Rmarkdown, pander and reports.

I now have two files:

'ReportIntro.brew' that contains the structure of the report

# My Report Title 
## Sample Information
#### <%=set.alignment('left') ; as.character(info[1,1])%>
<%=set.alignment('left') ; info[2:8,1:2]%>

'Report.R' to create a data.frame 'info' required for the report

library(pander) ; library(xlsx)
info=read.xlsx(file="info.xlsx", sheetName="info", header=FALSE)
Pandoc.brew(file="ReportIntro.brew", output=tempfile(), convert="docx")  

This gives me my first Word document, with a table. However, it includes unwanted row and column names. I found a blog about generating tables with pander and knitr, which suggested setting

row.names(info) <- NULL

but this had no effect.

If I try using

print(info[2:8,1:2], include.rownames=FALSE)

or

print(xtable(info[2:8,1:2]), type="html", include.rownames=FALSE)

as suggested in another post about removing row names using xtable, the table doesn't appear at all in the Word doc.

So: how do I get my table without showing row names?

(This is my first post, so I hope it fits the requirements!)

EDIT: This it the result of dput(info)

> dput(info)
structure(list(X1 = c("School Information", "Name", "Type", "DfE number", 
"URN", "DfE link", "Dashboard Report", "Ofsted link", "Test Information", 
"Test Date", "Comments", "Analysis comments", "Sample Size", 
"Year group", "All", "11", "11"), X2 = c(NA, NA, "Primary, Academy, Prep, Middle etc", 
NA, "Enter school URN here", "http://www.education.gov.uk/", 
"http://dashboard.ofsted.gov.uk/", "http://www.ofsted.gov.uk", 
NA, NA, NA, NA, NA, "Set", "All", "top", "middle"), X3 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "M", "408", "165", 
"243"), X4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "F", "402", "145", "257"), X5 = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "Total", "810", "310", "500")), .Names = c("X1", 
"X2", "X3", "X4", "X5"), row.names = c(NA, 17L), class = "data.frame")

回答1:


I am the author of pander thanks for giving it a try, @Jerubaal. It would be awesome if your could also post info or rather the result of dput(info), so that I could reproduce your steps, but as far as I see now a workaround would be to first save that subset of info into a new variable, then returning that new object in a chunk.

More details: pander suprresses row.names if those are trivial, like a sequence from 1 to the number of rows. If you return a subset of a data.frame, you return an object with row.names that do not fit this basic schedule. But if you create a new data.frame, the automatic row.names would start from 1 to nrow.

Quick demo (please note that pander is run automatically on each chunk anyway):

> library(pander)
> pander(iris[2:3, 1:3])

---------------------------------------------------
&nbsp;   Sepal.Length   Sepal.Width   Petal.Length 
------- -------------- ------------- --------------
 **2**       4.9             3            1.4      

 **3**       4.7            3.2           1.3      
---------------------------------------------------

> x <- iris[2:3, 1:3]
> pander(x)

---------------------------------------------------
&nbsp;   Sepal.Length   Sepal.Width   Petal.Length 
------- -------------- ------------- --------------
 **2**       4.9             3            1.4      

 **3**       4.7            3.2           1.3      
---------------------------------------------------

> row.names(x) <- NULL
> pander(x)

-------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length 
-------------- ------------- --------------
     4.9             3            1.4      

     4.7            3.2           1.3      
-------------------------------------------

Update: demo with the provided info dataset

> x <- info[2:8,1:2]
> pander(x)

--------------------------------------------------------
&nbsp;         X1                      X2               
------- ---------------- -------------------------------
 **2**        Name                                      

 **3**        Type           Primary, Academy, Prep,    
                                   Middle etc           

 **4**     DfE number                                   

 **5**        URN             Enter school URN here     

 **6**      DfE link      http://www.education.gov.uk/  

 **7**  Dashboard Report http://dashboard.ofsted.gov.uk/

 **8**    Ofsted link       http://www.ofsted.gov.uk    
--------------------------------------------------------

> row.names(x) <- NULL
> pander(x)

------------------------------------------------
       X1                      X2               
---------------- -------------------------------
      Name                                      

      Type           Primary, Academy, Prep,    
                           Middle etc           

   DfE number                                   

      URN             Enter school URN here     

    DfE link      http://www.education.gov.uk/  

Dashboard Report http://dashboard.ofsted.gov.uk/

  Ofsted link       http://www.ofsted.gov.uk    
------------------------------------------------


来源:https://stackoverflow.com/questions/21018646/how-do-i-remove-row-names-when-using-pander-to-brew-a-report

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!