【问题标题】:Convert a Bibtex class object to a series of text strings formatted for each citation将 Bibtex 类对象转换为为每个引用格式化的一系列文本字符串
【发布时间】:2020-09-30 07:27:18
【问题描述】:

我是 bibTex 对象的新手,想为对象中的每个引用生成一个文本字符串列表作为格式化引用。我不想生成文档——这些字符串将用于 R 中的下游用途。有没有办法做到这一点?我什至无法真正弄清楚如何访问 bibTex 对象中每个引用的片段。

换一种说法,我该怎么转:

   temp <- toBibtex(c(citation("base"), citation("sp")))

进入这个:

  [[1]]
  [1] "R Core Team (2019). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/."

  [[2]]
  [1] "Pebesma, E.J., R.S. Bivand (2005). Classes and methods for spatial data in R. R News 5. https://cran.r-project.org/doc/Rnews/."

  [[3]]
  [1] "Bivand, R.S., Pebesma, E.J., Gomez-Rubio, V. (2013). Applied spatial data analysis with R, Second edition. Springer, NY. https://asdar-book.org/."

帮助?

【问题讨论】:

  • 感谢@zx8754 和@agila 的回答。两者都非常合适且可行。在这种情况下,我选择了@zx8754,因为我希望对引用格式的控制比$textVersion 给我的更多。我真的很喜欢控件bib2df() 为您提供了过度格式化,我只是希望我不必先将 .bib 文件写入磁盘。啊,好吧。
  • 从 RefmanageR 包中添加了更多内容,我认为这是您需要探索的包,它不需要从 bib 文件中读取,例如,请参阅我的答案。
  • 这个传奇的另一个后续行动——显然bibtex 是孤立的并已被存档,而RefManageR 正在走向类似的命运。回到绘图板!

标签: r bibtex


【解决方案1】:

使用bib2df包:

library(bibtex)
library(bib2df)

write.bib(c("base", "sp"), "temp.bib")

x <- bib2df("temp.bib")

apply(x, 1, function(i){
  paste(
    #adding authors and titles
    paste(unlist(i$AUTHOR), collapse = ", "),
    i$TITLE,
    # add other bits here as needed
    sep = ", ")
})

# [1] "R Core Team, R: A Language and Environment for Statistical Computing"                                        
# [2] "Edzer J. Pebesma, Roger S. Bivand, Classes and methods for spatial data in {R"                               
# [3] "Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio, Applied spatial data analysis with {R}, Second edition"

注意:我也是 bibtex 的新手。还有 RefManageR 包,可能更有用。


使用RefManageR

library(RefManageR)

# read bib file
x1 <- ReadBib("temp.bib")
# or convert citations to bibentry object
x2 <- as.BibEntry(c(citation("base"), citation("sp")))

两者都将打印如下:

# [1] R. S. Bivand, E. Pebesma, and V. Gomez-Rubio. _Applied spatial data analysis with R, Second
# edition_. Springer, NY, 2013. <URL: https://asdar-book.org/>.
# 
# [2] E. J. Pebesma and R. S. Bivand. “Classes and methods for spatial data in R”. In: _R News_ 5.2 (Nov.
#                                                                                                    2005), pp. 9-13. <URL: https://CRAN.R-project.org/doc/Rnews/>.
# [3] R Core Team. _R: A Language and Environment for Statistical Computing_. R Foundation for
# Statistical Computing. Vienna, Austria, 2019. <URL: https://www.R-project.org/>.

【讨论】:

    【解决方案2】:

    我认为您可以应用以下代码:

    pkgs <- c("base", "sp")
    lapply(pkgs, function(x) citation(x)$textVersion)
    #> [[1]]
    #> [1] "R Core Team (2020). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/."
    #> 
    #> [[2]]
    #> [[2]][[1]]
    #> [1] "Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial data in R. R News 5 (2), https://cran.r-project.org/doc/Rnews/."
    #> 
    #> [[2]][[2]]
    #> [1] "Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio, 2013. Applied spatial data analysis with R, Second edition. Springer, NY. https://asdar-book.org/"
    

    或者,如果您需要为列表的每个元素精确提供 1 个引用,我认为您可以运行:

    as.list(unlist(lapply(pkgs, function(x) citation(x)$textVersion)))
    #> [[1]]
    #> [1] "R Core Team (2020). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/."
    #> 
    #> [[2]]
    #> [1] "Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial data in R. R News 5 (2), https://cran.r-project.org/doc/Rnews/."
    #> 
    #> [[3]]
    #> [1] "Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio, 2013. Applied spatial data analysis with R, Second edition. Springer, NY. https://asdar-book.org/"
    

    reprex package (v0.3.0) 于 2020 年 9 月 30 日创建

    【讨论】:

    • 不一定是bib 对象,但$textVersion 引用的格式存在相当大的差异。我希望能够重新格式化引文以使其统一。
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2017-02-07
    相关资源
    最近更新 更多