在R Sweave

时间:2016-09-06 12:45:49

标签: r sweave

需要你帮助解决一个小问题,我使用R Sweave生成动态报告,代码是:

industryTable<-data.frame("Industry" = INDUSTRY.DATA[1:15,]$industryname,
                      "Freq" = INDUSTRY.DATA[1:15,]$Freq,
                      "Perc" = INDUSTRY.DATA[1:15,]$Perc,
                      "Industry" = INDUSTRY.DATA[16:30,]$industryname,
                      "Freq" = INDUSTRY.DATA[16:30,]$Freq,
                      "Perc" = INDUSTRY.DATA[16:30,]$Perc)
names(industryTable)<-c("Industry", "Freq","Perc","Industry", "Freq","Perc")
add.to.row <- list(pos = list(nrow(industryTable)), command = NULL)
comm <- paste0("\\hline \n \\multicolumn{",dim(industryTable)[2],"}{l}",
               "{\\scriptsize{Matching-Method:Pscore, Base-Year:0,Matching-Interval:0.9-1.10,Log:T,Trim:F}} \n")
add.to.row$command <-comm
print(xtable(industryTable,caption = "Distribution of Privatization Across Manufacturing", label = "table:industry",align = c("c","p{4.5cm}","c","c","|p{4.5cm}","c","c"),digits = c(0,0,0,2,0,0,2)),caption.placement="top", include.rownames = FALSE,add.to.row = add.to.row,hline.after=c(-1, 0))

我需要在表格的底部放置并注意我所做的事情。目前正在使用add.to.row

add.to.row <- list(pos = list(nrow(industryTable)), command = NULL)
    comm <- paste0("\\hline \n \\multicolumn{",dim(industryTable)[2],"}{l}",
                   "{\\scriptsize{Matching-Method:Pscore, Base-Year:0,Matching-Interval:0.9-1.10,Log:T,Trim:F}} \n") 

它运行正常,但是,我在每个32个版本的数据组合中有7个表需要完全相同的注释格式,如此处的表格,表格内容和格式不同,但对于给定数据组合的7个表格,表格注释完全相同,即&#34;匹配方法:Pscore,Base-Year:0,Matching-Interval:0.9-1.10,Log:T,Trim:F&#34;,是否有方便的方法在没有复制粘贴的情况下实现这一点,导致它繁琐且危险。

1 个答案:

答案 0 :(得分:0)

为什么不创建一个函数?

make_table <- function(industryTable) {
   l =  list(pos = list(nrow(industryTable)), command = NULL)
    comm <- paste0("\\hline \n \\multicolumn{",dim(industryTable)[2],"}{l}",
                   "{\\scriptsize{Matching-Method:Pscore, Base-Year:0,Matching-Interval:0.9-1.10,Log:T,Trim:F}} \n")
   l$command <-comm
   xtable(industryTable, 
           caption = "Distribution of Privatization Across Manufacturing", 
           label = "table:industry",align = c("c","p{4.5cm}","c","c","|p{4.5cm}","c","c"),digits = c(0,0,0,2,0,0,2)), 
           caption.placement="top", 
           include.rownames = FALSE, 
           add.to.row = l, hline.after=c(-1, 0)
}

所以

print(make_table(industryTable))