如何用R在Quarto网站自动生成符合要求的出版物列表?
问题
我打算用Quarto搭建新的学术网站,核心目标是减少周/月度维护工作量,让网站同时具备学术展示和基础CV功能,并且能通过Quarto整合.qmd文件生成PDF版CV。
另外,我需要用R从bib文件自动生成符合以下要求的出版物列表:
- 按
@article和@book类型分类展示 - 每个分类内按年份降序排列(2022→2021→2020)
- 每个分类下的出版物要添加编号,比如
@book类第一篇标[1]、第二篇标[2] - 多作者时,高亮(加粗)我的名字(比如John Doe)
附示例bib文件条目:
@article{Doe2022b, author = {Doe, John}, date-added = {2022-11-20 09:36:34 +0100}, date-modified = {2022-11-20 09:37:52 +0100}, doi = {10.1109/5.771073}, journal = {Daily Prophet}, number = {3}, pages = {1-25}, title = {I still know it better}, volume = {25}, year = {2020}, bdsk-url-1 = {https://doi.org/10.1109/5.771073}} @article{Doe2020, author = {Doe, Jane and Doe, John}, date-added = {2022-11-20 09:34:29 +0100}, date-modified = {2022-11-20 09:36:30 +0100}, doi = {10.1109/5.771073}, journal = {Daily Prophet}, number = {1}, pages = {18-35}, title = {We know it better}, volume = {23}, year = {2020}} @book{Doe2021, address = {Hogwarts}, author = {Doe, John}, date-added = {2022-11-20 09:34:11 +0100}, date-modified = {2022-11-20 09:34:19 +0100}, publisher = {Flourish and Blotts}, title = {My first Book}, year = {2021}} @book{Doe2022, address = {Hogwarts}, author = {Doe, John}, date-added = {2022-11-20 09:32:15 +0100}, date-modified = {2022-11-20 09:33:56 +0100}, publisher = {Flourish and Blotts}, title = {My second Book}, year = {2022}}
解决方案
一、Quarto网站与PDF CV整合
Quarto支持同一源文件生成多格式输出,按以下步骤实现:
- 把CV内容放在单独的
cv.qmd文件中,可使用Quarto官方CV模板(执行quarto use template quarto-journals/resume)或自定义格式。 - 在网站的
_quarto.yml中添加该文件到导航栏,同时在cv.qmd的YAML头部指定PDF输出配置:
--- title: "John Doe - CV" format: html: include-in-site: true # 让CV页面嵌入学术网站 pdf: documentclass: article toc: false ---
后续更新CV只需修改cv.qmd,网站页面和PDF版本会同步生成,无需重复维护。
二、用R自动生成格式化出版物列表
借助bib2df读取bib数据,结合dplyr、purrr处理格式,具体实现如下:
1. 安装并加载依赖包
install.packages(c("bib2df", "dplyr", "purrr", "stringr")) library(bib2df) library(dplyr) library(purrr) library(stringr)
2. 读取并处理bib数据
# 读取目标bib文件 bib_df <- bib2df("your_publications.bib") # 定义需要高亮的个人姓名 my_name <- "Doe, John" # 数据清洗与格式化 processed_bib <- bib_df %>% filter(TYPE %in% c("ARTICLE", "BOOK")) %>% # 仅保留文章和书籍类型 mutate(YEAR = as.integer(YEAR)) %>% arrange(TYPE, desc(YEAR)) %>% # 按类型分组、年份降序排序 group_by(TYPE) %>% mutate(item_number = row_number()) %>% # 分类内生成序号 ungroup() %>% # 格式化作者列表,高亮个人姓名 mutate( formatted_authors = map_chr(AUTHOR, function(authors) { authors %>% str_replace_all(my_name, paste0("**", my_name, "**")) %>% paste(collapse = ", ") }), # 按类型生成标准化条目 formatted_entry = case_when( TYPE == "ARTICLE" ~ paste0( "[", item_number, "] ", formatted_authors, ". (", YEAR, "). ", "**", TITLE, "**. *", JOURNAL, "*, ", VOLUME, "(", NUMBER, "), ", PAGES, ". ", ifelse(!is.na(DOI), paste0("https://doi.org/", DOI), "") ), TYPE == "BOOK" ~ paste0( "[", item_number, "] ", formatted_authors, ". (", YEAR, "). ", "**", TITLE, "**. ", ADDRESS, ": ", PUBLISHER, "." ) ) )
3. 在Quarto中调用展示
在学术网站的出版物页面(如publications.qmd)中,添加以下R代码块(设置echo: false隐藏代码):
# 按类型分组输出出版物列表 processed_bib %>% group_by(TYPE) %>% group_walk(function(data, type) { cat(paste0("## ", str_to_title(type[[1]]), "s\n\n")) # 输出分类标题 cat(paste0("- ", data$formatted_entry, collapse = "\n")) # 输出带序号的条目 cat("\n\n") })
执行后会自动生成:
- 分类标题(Articles、Books)
- 每个分类下按年份降序排列的带编号条目,个人姓名自动加粗高亮
三、自动化维护
将上述R代码嵌入Quarto页面后,每次渲染网站时,Quarto会自动读取最新的bib文件并更新出版物列表,完全无需手动编辑条目。
内容的提问来源于stack exchange,提问作者feder80
相关产品推荐
相关产品推荐

