1

我有 .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]])
}
4

2 回答 2

1

我认为您最简单的选择是将 .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结构并不完全相同,因此很难从一种上下文转换到另一种上下文。)

于 2018-05-24T06:35:00.893 回答
0

尝试像这样更改代码(确保使用read.bib函数并在循环中引用要更改的文本出现的字段,例如“note”或“title”。对于@andrew_reece 提供的示例文件,它应该像这样工作:

refs = read.bib("example.bib", encoding = "unknown", srcfile)

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

WriteBib(as.BibEntry(refs), "example2.bib")

但是,根据您的任务描述,我同意@andrew_reece 将 bib 文件视为纯文本更容易(另一方面,对于较大的 bib 文件,您实际上可能希望更好地控制要替换的字段。)

于 2018-05-24T07:05:30.487 回答