如何从R语言exams包的嵌套考试列表中提取习题名称?
问题描述
我使用R语言的exams包创建了8份考试(每份对应一个*.rds文件),每份考试包含相同的8道习题但顺序不同。所有考试的*.rds文件已合并为一个嵌套列表exams_list,示例结构如下:
exams_list <- list(`00001` = list(exercise1 = list(question = c("\\emph{(2 \\myPoints)}", "Q1"), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_descriptive_stat_34", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_34", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, FALSE, TRUE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_34: b", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 40283L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise1")), exercise2 = list(question = c("\\emph{(2 \\myPoints)}", "Q2", "", "\\hfill\\break", "\\includegraphics[width=0.55\\textwidth,height=\\textheight]{stat1_descriptive_stat_10.png}" ), questionlist = c("A1", "A2","A3", "A4", "A5" ), solution = NULL, solutionlist = NULL, metainfo = list( file = "stat1_en_descriptive_stat_10", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_10", title = NULL, section = NULL, version = NULL, solution = c(FALSE, TRUE, FALSE, FALSE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_10: b", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 23616L), supplements = structure(c(stat1_descriptive_stat_10.png = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise2/stat1_descriptive_stat_10.png"), dir = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise2")), exercise3 = list(question = c("\\emph{(2 \\myPoints)}", "Q3" ), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list( file = "stat1_en_descriptive_stat_15", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_15", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, FALSE, TRUE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_15: d", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 84038L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise3")), exercise4 = list(question = c("\\emph{(2 \\myPoints)}", "Q4"), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_descriptive_stat_29", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_29", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, FALSE, TRUE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_29: d", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 104204L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise4")), exercise5 = list(question = c("\\emph{(2 \\myPoints)}", "Q5" ), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_prob_8", markup = "latex", type = "schoice", name = "stat1_en_prob_8", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, TRUE, FALSE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_prob_8: c", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 99878L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file808555d833f/exam1/exercise5"))), `00002` = list(exercise1 = list(question = c("\\emph{(2 \\myPoints)}", "Q1"), questionlist = c("A1", "A2","A3", "A4", "A5" ), solution = NULL, solutionlist = NULL, metainfo = list( file = "stat1_en_descriptive_stat_10", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_10", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, TRUE, FALSE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_10: c", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 66957L), supplements = structure(c(stat1_descriptive_stat_10.png = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise1/stat1_descriptive_stat_10.png"), dir = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise1")), exercise2 = list(question = c("\\emph{(2 \\myPoints)}", "Q2"), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_descriptive_stat_34", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_34", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, FALSE, TRUE, FALSE ), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_34: a", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 24446L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise2")), exercise3 = list(question = c("\\emph{(2 \\myPoints)}", "Q3"), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_descriptive_stat_29", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_29", title = NULL, section = NULL, version = NULL, solution = c(FALSE, FALSE, TRUE, FALSE, FALSE ), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_29: c", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 42982L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise3")), exercise4 = list(question = c("\\emph{(4 \\myPoints)}", "Q4" ), questionlist = c("A1", "A2","A3", "A4", "A5"), solution = NULL, solutionlist = NULL, metainfo = list(file = "stat1_en_prob_8", markup = "latex", type = "schoice", name = "stat1_en_prob_8", title = NULL, section = NULL, version = NULL, solution = c(FALSE, TRUE, FALSE, FALSE, FALSE ), clozetype = NULL, points = 4, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_prob_8: b", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 80660L), supplements = structure(c(stat1_en_descriptive_stat_20_1.png = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise4/stat1_en_prob_8_1.png", stat1_en_descriptive_stat_20_2.png = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise4/stat1_en_prob_8_2.png" ), dir = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise4")), exercise5 = list(question = c("\\emph{(2 \\myPoints)}", "Q5" ), questionlist = c("A1", "A2","A3", "A4", "A5" ), solution = NULL, solutionlist = NULL, metainfo = list( file = "stat1_en_descriptive_stat_15", markup = "latex", type = "schoice", name = "stat1_en_descriptive_stat_15", title = NULL, section = NULL, version = NULL, solution = c(FALSE, TRUE, FALSE, FALSE, FALSE), clozetype = NULL, points = 2, time = NULL, shuffle = 5, length = 5L, string = "stat1_en_descriptive_stat_15: b", maxchars = NULL, abstention = NULL, stringtype = NULL, seed = 69316L), supplements = structure(character(0), names = character(0), dir = "/tmp/RtmpH83QPG/file8087677e29b/exam1/exercise5"))))
需要提取的是每份考试中各习题的名称,对应路径如下:
exams_list$`00001`$exercise1$metainfo$name exams_list$`00001`$exercise2$metainfo$name #... exams_list$`00002`$exercise1$metainfo$name exams_list$`00002`$exercise2$metainfo$name
目标是得到一个列表,每个元素为对应考试的习题名称向量,我尝试用purrr包但未成功。
解决方案
方法1:使用purrr包实现
利用purrr的嵌套映射函数,外层遍历每份考试,内层遍历考试中的每个习题,提取metainfo$name并转为字符向量:
library(purrr) # 提取每份考试的习题名称向量 exercise_names <- map(exams_list, ~ map_chr(.x, ~ .x$metainfo$name))
代码解释
- 外层
map():遍历exams_list中的每个考试(如00001、00002),返回一个新列表。 - 内层
map_chr():遍历当前考试下的所有习题,提取每个习题的metainfo$name,并返回字符向量(而非列表)。 - 最终
exercise_names就是目标列表,每个元素对应一份考试的习题名称向量。
方法2:基础R实现(无需额外包)
如果不想使用purrr,可以用基础R的lapply()和sapply()组合实现:
# 基础R版本 exercise_names_base <- lapply(exams_list, function(exam) { sapply(exam, function(exercise) exercise$metainfo$name) })
结果验证
运行代码后,可通过以下方式查看结果:
# 查看第一份考试的习题名称 exercise_names$`00001`
输出示例:
[1] "stat1_en_descriptive_stat_34" "stat1_en_descriptive_stat_10" [3] "stat1_en_descriptive_stat_15" "stat1_en_descriptive_stat_29" [5] "stat1_en_prob_8"
内容的提问来源于stack exchange,提问作者Sinval
相关产品推荐
相关产品推荐

