R 将gtables与循环中的单独标题组合在一起

R 将gtables与循环中的单独标题组合在一起,r,R,我有学生考试成绩的数据。每个学生参加一次100道题的考试。每个问题都被(不平等地)分配到5个领域中的一个,并且有一个与之相关的独特的学习结果。我需要在表格中总结数据(可能分布在多个pdf页面上),确定每个领域的学习结果,学生得到的正确/错误 对于每个领域,我都设法得到了一个表,其中显示了学生得到的正确/错误的学习结果,但我不知道如何在循环后将这些表连接在一起。我真的很感激你能帮我 请在下面找到我的代码副本: library(tidyverse) library(dplyr) library(g

我有学生考试成绩的数据。每个学生参加一次100道题的考试。每个问题都被(不平等地)分配到5个领域中的一个,并且有一个与之相关的独特的学习结果。我需要在表格中总结数据(可能分布在多个pdf页面上),确定每个领域的学习结果,学生得到的正确/错误

对于每个领域,我都设法得到了一个表,其中显示了学生得到的正确/错误的学习结果,但我不知道如何在循环后将这些表连接在一起。我真的很感激你能帮我

请在下面找到我的代码副本:

library(tidyverse)
library(dplyr)

library(gridExtra)
library(grid)
library(gtable)
library(ids)

# Create some random Learning Objectives:
LO <- ids::adjective_animal(2500, 2, style = "sentence")

# Create data:
testdata <- data.frame(cbind(StudentID=rep(1:25,each=100),
                             QuestionNumber=rep(1:100),
                             Correct=sample(c(0,1),replace=TRUE,size=2500),
                             Domain=c("A","A","A","B","C","C","C","D","D","E"),
                             LearningOutcome=LO))
rm(LO)

# Create lists for Loops:
DomainList <- unique(testdata$Domain)
StudentList <- unique(testdata$StudentID)

Domain.Colours <- list("steelblue3","red2","violetred3","forestgreen","chocolate1")


for (i in 1:length(unique(testdata$StudentID))){  
  
  for (j in 1:length(DomainList)){
    
    # Select Domain Specfic Data
    TEMP.DatabyDomain <- testdata[testdata$Domain==DomainList[j],]
    
    # Split Learning Outcomes into Correct/Incorrect Lists
    TEMP.Correct   <- TEMP.DatabyDomain$LearningOutcome[TEMP.DatabyDomain$StudentID == StudentList[i] & TEMP.DatabyDomain$Correct == 1]
    TEMP.Incorrect <- TEMP.DatabyDomain$LearningOutcome[TEMP.DatabyDomain$StudentID == StudentList[i] & TEMP.DatabyDomain$Correct == 0]
    
    # Update the lengths to be the same
    n <- max(length(TEMP.Correct), length(TEMP.Incorrect))
    length(TEMP.Correct) <- n                      
    length(TEMP.Incorrect) <- n
    
    # Combine the data into a new df
    FeedbackData <- data.frame(TEMP.Correct, TEMP.Incorrect)
    FeedbackData <- sapply(FeedbackData, as.character)
    
    # Replace NAs with ""
    FeedbackData[is.na(FeedbackData)] <- " "
    
    # Create column headings
    colnames(FeedbackData) <- c(paste("Correctly answered questions\n in domain:",DomainList[j]),paste("Incorrectly answered questions\n in domain:",DomainList[j]))
    
    # Table Settings
    tt1 <- ttheme_default(core=list(fg_params=list(fontsize=8)),
                          rowhead=list(fg_params=list(hjust=0, x=0)),
                          colhead=list(bg_params=list(fill=paste(Domain.Colours[j])),
                                       fg_params=list(col="white")))
    
    # Table Results
    tfeedback <- tableGrob(FeedbackData, theme=tt1, rows = NULL)
    
    }
    
    ### Here is where I get stuck ###
    # I would like to append the 5 Domain tables together, so that they are one long table with the column headings in-between
  
    tfeedbacktitle <- textGrob(paste("Feedback for Students"),gp=gpar(fontsize=20))
    padding <- unit(10,"mm")

    table <- gtable_add_rows(
      tfeedback, 
      heights = grobHeight(tfeedbacktitle) + padding,
      pos = 0)
    table <- gtable_add_grob(
      table, 
      tfeedbacktitle, 
      1, 1, 1, ncol(table))  

    # Code below to allow table to cover multiple pages
    # Taken from <https://stackoverflow.com/questions/15937131/print-to-pdf-file-using-grid-table-in-r-too-many-rows-to-fit-on-one-page>
    fullheight <- convertHeight(sum(table$heights), "cm", valueOnly = TRUE)
    margin <- unit(0,"in")
    margin_cm <- convertHeight(margin, "cm", valueOnly = TRUE)
    a4height <- 29.7 - margin_cm
    nrows <- nrow(table)
    npages <- ceiling(fullheight / a4height)
    
    heights <- convertHeight(table$heights, "cm", valueOnly = TRUE) 
    rows <- cut(cumsum(heights), include.lowest = FALSE,
                breaks = c(0, cumsum(rep(a4height, npages))))
    
    groups <- split(seq_len(nrows), rows)
    
    gl1 <- lapply(groups, function(id) table[id,])
    
    mypath <- file.path(pathOutput,paste("StudentID_", StudentList[i], ".pdf", sep = ""))
    
    pdf(file=mypath, paper = "a4", width = 0, height = 0)
    for(page in seq_len(npages)){
      grid.newpage()
      grid.rect(width=unit(21,"cm") - margin,
                height=unit(29.7,"cm")- margin)
      grid.draw(gl1[[page]])
    }
    
    dev.off()
}
库(tidyverse)
图书馆(dplyr)
图书馆(gridExtra)
图书馆(网格)
图书馆(gtable)
图书馆(ids)
#创建一些随机的学习目标:

对不起,Ronak,我尽量保持最低限度,这样你就可以看到我需要做什么。代码中生成了testdata。代码为每个学生生成一个pdf文件,但它只包含1个域表。我希望每个学生的pdf都能包含所有表格,一个放在另一个上面,根据需要跨多个页面。我现在在我的帖子中添加了一个示例图片。我想你可能想阅读这个问题-答案应该会帮助你感谢Jessi,但我不知道这会如何帮助我将tfeedback表组合成一个表。哦……我以为你希望每个反馈表(包含尽可能多的表)都有标题。