ropensci / bibtex

bibtex parser for R
https://docs.ropensci.org/bibtex/
35 stars 12 forks source link

Parse single entry from string #35

Open dmi3kno opened 3 years ago

dmi3kno commented 3 years ago

I am fetching citations from Zotero with BetterBibtex-interfacing {rbbt} by @paleolimbot. I would love to be able to edit bibtex entry before it gets written to ".bib" file, i.e. while it is still in string format.

Ideally I would do that with {RefManageR}, but currently {RefManageR} relies on {bibtex} to parse .bib. If {bibtex} had an exposed function for reading literal bib string, then I could use {RefManageR} to edit it. Would you consider adding a parser for literal bibtex?

my_ref <- " @book{McElreath_2020, edition={2}, 
   title={Statistical Rethinking: A Bayesian Course with Examples in R and Stan}, ISBN={978-0-429-02960-8},
   url={https://www.taylorfrancis.com/books/9780429642319}, DOI={10.1201/9780429029608}, 
   publisher={Chapman and Hall/CRC}, author={McElreath, Richard}, year={2020}, month={Mar} }"

my_bibentry <- read.bib(text=my_ref)

It needs to be vectorized, of course, i.e. accepting character vectors of length()>1.

Thank you for providing such an important infrastructure package for bibliography management infrastructure in R.

dmi3kno commented 3 years ago

Related to #1

trashbirdecology commented 3 years ago

@dmi3kno, I too came here to try to find guidance on reading in .bib files, but it looks like read.bib is designed to only read .bib from inside packages, and not from .bib files directly.

See package 'bibliometrix' for reading in bib from specific sources, but I am still trying to find someone who wrote code to read in basic bib. https://warin.ca/shiny/bibliometrix/#section-import-convert

dmi3kno commented 3 years ago

There are some names and addresses in the vignettes. I still believe it is possible to wrap the underlying C library in such a way that would make it possible to read a character vector with bib content (from file from disk). At least generalizing the connection (to enable textConnection, as Romain implies in #1).

dieghernan commented 2 years ago

Hi:

You can write the string to a temporal bib file and read it with read.bib(). This is already implemented on a test:

https://github.com/ropensci/bibtex/blob/1c0e12d0fbf9aefea4e72c4aef1c40b0b972cf6a/tests/testthat/test-bibtex.R#L14-L20

And a full reprex with your string:

my_ref <- " @book{McElreath_2020, edition={2},
   title={Statistical Rethinking: A Bayesian Course with Examples in R and Stan}, ISBN={978-0-429-02960-8},
   url={https://www.taylorfrancis.com/books/9780429642319}, DOI={10.1201/9780429029608},
   publisher={Chapman and Hall/CRC}, author={McElreath, Richard}, year={2020}, month={Mar} }"

tmp1 <- tempfile(fileext = ".bib")

writeLines(my_ref, tmp1)

library(bibtex)

bib <- read.bib(tmp1)

bib
#> McElreath R (2020). _Statistical Rethinking: A Bayesian Course with
#> Examples in R and Stan_, 2 edition. Chapman and Hall/CRC. ISBN
#> 978-0-429-02960-8, doi: 10.1201/9780429029608 (URL:
#> https://doi.org/10.1201/9780429029608), <URL:
#> https://www.taylorfrancis.com/books/9780429642319>.

toBibtex(bib)
#> @Book{McElreath_2020,
#>   edition = {2},
#>   title = {Statistical Rethinking: A Bayesian Course with Examples in R and Stan},
#>   isbn = {978-0-429-02960-8},
#>   url = {https://www.taylorfrancis.com/books/9780429642319},
#>   doi = {10.1201/9780429029608},
#>   publisher = {Chapman and Hall/CRC},
#>   author = {Richard McElreath},
#>   year = {2020},
#>   month = {Mar},
#> }

Created on 2021-12-09 by the reprex package (v2.0.1)