【问题标题】:Generate summary table and one page report for each object为每个对象生成汇总表和一页报告
【发布时间】:2017-04-21 22:31:16
【问题描述】:

这是一个更笼统的问题,因为我不知道从哪里开始。我不是老师,但这是我将使用的示例,因为它是相关的。我正在使用 R 和 Sweave 来生成这些报告。

假设我是一名有 6 个班级的老师,我想为每个班级生成一份报告。每份报告都以一个汇总表开头,其中列出了班级中的每个学生以及他们在当前期间和以前期间的成绩。在每份报告的后续页面中,都有一份关于当前评分期间每个学生的一页摘要,其中包含一些图表和家庭作业、测验、测试等的分数。

我使用this answer 来生成循环报告非常成功,并且可能会通过生成摘要页面来开始每个报告。但是,我对如何获取每个学生的子报告有点困惑。非常感谢有关搜索内容或在线文章的任何建议。此外,如果您对我的一般性问题有一般性建议,我们也非常感谢。

【问题讨论】:

    标签: r sweave


    【解决方案1】:

    对于看到这个的任何人,关键是谷歌主子编织文档。这是一个普遍的答案:

    classes.r

    library(knitr)
    library(tidyverse)
    
    class_scores <- data.frame(class = as.factor(rep(1:5, 520)),
                           student = rep(letters, 100),
                           score = runif(2600, min = 50, max = 100))
    for (i in 1:5){
      class_sub <- class_scores %>%
        filter(class == i)
      title <- paste0("class", i, ".tex")
      knit2pdf("master_class.rnw", title)
    }
    

    master_class.rnw

    \documentclass{article}
    
    \begin{document}
    
    Here's the report for Class \Sexpr{i}
    
    \clearpage
    
    \section{Summary}
    
    The Summary of the Class.
    <<master_summary, include = FALSE>>=
      summary <- class_sub %>%
        group_by(student) %>%
        summarize(score = mean(score))
    @
    
    \Sexpr{kable(summary)}
    
    \section{Student Reports}
    
    Here's the section for each Student's report
    
    \clearpage
    
    <<master_loop, include = FALSE>>=
    
      student_out <- NULL
    
      for (let in letters) {
        student_out <- c(student_out, knit_child("student_report.rnw"))
      }
    @
    
    \Sexpr{paste(student_out, collapse = "\n")}
    
    End Class Report
    
    \end{document}
    

    student_report.rnw

    Here is the report for student \Sexpr{let}
    
    %note: don't name these code chunks!
    
    <<include = FALSE>>=
      student <- class_sub %>%
        filter(student == let)
    
      p <- ggplot(student, aes(x = score)) +
        geom_histogram()
    @
    
    \Sexpr{summary(student$score)}
    
    <<echo = FALSE>>=
      p
    @
    
    End Student Report
    
    \clearpage
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2021-11-23
      • 2016-12-04
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多