首页 > 解决方案 > 使用摘要从 R 编写 .bib 文件

问题描述

我有 .bib 文件(从 web of science 下载),我想将其导入 R,用“CONSIDERING”替换“in light of”的所有实例,并将其导出为 .bib 文件。我一直无法找到可以将我的数据写回 .bib 文件的函数。WriteBib 不起作用,因为 refs 是“pairlist”对象,而不是“bibentry”。关于如何导出可以导入 Mendeley 的 .bib 文件的任何建议?感谢您的帮助!

这是代码:

library(bibtex)
library(RefManageR)

refs = do_read_bib("/Users/CarrieAnn/Downloads/savedrecs (1).bib", encoding = "unknown", srcfile)

for (i in 1:length(refs)) {
  refs[[i]] = gsub("in light of", "CONSIDERING", refs[[i]])
}

标签: rbibtexmendeley

解决方案


我认为您最简单的选择是将 .bib 文件视为普通文本文件。尝试这个:

raw_text  <- readLines("example.bib")
new_text  <- gsub("in light of", "CONSIDERING", raw_text)
writeLines(new_text, con="new_example.bib")

内容example.bib

%  a sample bibliography file
%  

@article{small,
author = {Doe, John},
title = {A small paper},
journal = {The journal of small papers},
year = 1997,
volume = {-1},
note = {in light of recent events},
}

@article{big,
author = {Smith, Jane},
title = {A big paper},
journal = {The journal of big papers},
year = 7991,
volume = {MCMXCVII},
note = {in light of what happened},
}

输出new_example.bib

%  a sample bibliography file
%  

@article{small,
author = {Doe, John},
title = {A small paper},
journal = {The journal of small papers},
year = 1997,
volume = {-1},
note = {CONSIDERING recent events},
}

@article{big,
author = {Smith, Jane},
title = {A big paper},
journal = {The journal of big papers},
year = 7991,
volume = {MCMXCVII},
note = {CONSIDERING what happened},
}

一点解释:
BibEntry对象具有非标准的内部结构,并且在RefManageR包中提供的功能之外很难使用。一旦你unclass或将一个对象简化为一个列表,由于对象所需的字段和属性的混合,BibEntry就很难将其放回格式。bib(更糟糕的是,bibtex内部RefManageR结构并不完全相同,因此很难从一种上下文转换到另一种上下文。)


推荐阅读