使用摘要从R编写.bib文件

时间:2018-05-24 01:53:02

标签: r bibtex mendeley

我有.bib文件(从科学网上下载),我想将其导入R,用“CONSIDERING”替换“in of light”的所有实例,并将其导出为.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]])
}

2 个答案:

答案 0 :(得分:0)

我认为最简单的选择是将.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格式。 (更糟糕的是,bibtexRefManageR没有完全相同的内部结构,因此很难从一个上下文转换为另一个上下文。)

答案 1 :(得分:0)

尝试更改这样的代码(确保使用read.bib功能,并在循环中引用要更改的文本所在的字段,例如&#34; note&#34;或&# 34; title&#34;。对于@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文件,您实际上可能希望更多地控制您替换哪些字段。)