Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse single entry from string #35

Open
dmi3kno opened this issue Oct 20, 2020 · 4 comments
Open

Parse single entry from string #35

dmi3kno opened this issue Oct 20, 2020 · 4 comments

Comments

@dmi3kno
Copy link

dmi3kno commented Oct 20, 2020

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
Copy link
Author

dmi3kno commented Oct 20, 2020

Related to #1

@trashbirdecology
Copy link

trashbirdecology commented Nov 13, 2020

@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
Copy link
Author

dmi3kno commented Nov 13, 2020

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
Copy link
Member

Hi:

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

test_that("read.bib can use ? in key (#9)", {
tmp <- tempfile(fileext = ".bib")
entry <- "@Misc{key?,\n author = \"Smith, Bob\",\n title = \"The Title\",\n year = 2012, \n}"
writeLines(entry, tmp)
out <- read.bib(tmp)
expect_equal( names(out), "key?" )
})

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants