ropensci / handlr

convert among citation formats
https://docs.ropensci.org/handlr
Other
38 stars 4 forks source link

write handlr list with multiple citations to bibtex file #23

Closed taylorreiter closed 3 years ago

taylorreiter commented 3 years ago

I have successfully used handlr to convert between bibilography file formats (yay!), and I can print my bibtex to teh console using bibtex_write. How do I write this to a text file external to R?

File: references.json.txt

library(handlr)
tmp <- citeproc_reader("references.json.txt")
bibtex_writer(tmp) # prints to console as a list

I tried an lapply approach and was unsuccesful:

lapply(tmp, function(x) write.table(x, 'test.bib', append= T, sep='\n', quote = F))
Session Info ``` > session_info() ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────── setting value version R version 4.0.2 (2020-06-22) os macOS Catalina 10.15.2 system x86_64, darwin17.0 ui RStudio language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz America/Los_Angeles date 2020-10-30 ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────── package * version date lib source abind 1.4-5 2016-07-21 [1] CRAN (R 4.0.0) ade4 1.7-15 2020-02-13 [1] CRAN (R 4.0.0) AnnotationDbi * 1.50.3 2020-07-25 [1] Bioconductor ape 5.4-1 2020-08-13 [1] CRAN (R 4.0.2) assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0) backports 1.1.10 2020-09-15 [1] CRAN (R 4.0.2) Biobase * 2.48.0 2020-04-27 [1] Bioconductor BiocGenerics * 0.34.0 2020-04-27 [1] Bioconductor BiocManager 1.30.10 2019-11-16 [1] CRAN (R 4.0.0) BiocParallel 1.22.0 2020-04-27 [1] Bioconductor biomformat 1.16.0 2020-04-27 [1] Bioconductor Biostrings 2.56.0 2020-04-27 [1] Bioconductor bit 4.0.4 2020-08-04 [1] CRAN (R 4.0.2) bit64 4.0.5 2020-08-30 [1] CRAN (R 4.0.2) bitops 1.0-6 2013-08-17 [1] CRAN (R 4.0.0) blob 1.2.1 2020-01-20 [1] CRAN (R 4.0.0) bookdown 0.20 2020-06-23 [1] CRAN (R 4.0.2) boot 1.3-25 2020-04-26 [1] CRAN (R 4.0.2) broom * 0.7.0 2020-07-09 [1] CRAN (R 4.0.2) callr 3.4.4 2020-09-07 [1] CRAN (R 4.0.2) car 3.0-9 2020-08-11 [1] CRAN (R 4.0.2) carData 3.0-4 2020-05-22 [1] CRAN (R 4.0.0) caret * 6.0-86 2020-03-20 [1] CRAN (R 4.0.0) cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.0.0) class 7.3-17 2020-04-26 [1] CRAN (R 4.0.2) cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0) cluster 2.1.0 2019-06-19 [1] CRAN (R 4.0.2) clusterProfiler * 3.16.0 2020-04-27 [1] Bioconductor codetools 0.2-16 2018-12-24 [1] CRAN (R 4.0.2) colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.0) ComplexUpset * 0.5.15 2020-07-10 [1] Github (krassowski/complex-upset@f5dbafe) cowplot * 1.1.0 2020-09-08 [1] CRAN (R 4.0.2) crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0) crosstalk 1.1.0.1 2020-03-13 [1] CRAN (R 4.0.0) crul 1.0.0 2020-07-30 [1] CRAN (R 4.0.2) curl 4.3 2019-12-02 [1] CRAN (R 4.0.0) data.table 1.13.0 2020-07-24 [1] CRAN (R 4.0.2) DBI 1.1.0 2019-12-15 [1] CRAN (R 4.0.0) dbplyr 1.4.4 2020-05-27 [1] CRAN (R 4.0.0) DelayedArray 0.14.1 2020-07-14 [1] Bioconductor deldir 0.1-29 2020-09-13 [1] CRAN (R 4.0.2) DEoptimR 1.0-8 2016-11-19 [1] CRAN (R 4.0.0) desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0) destiny * 3.2.0 2020-04-27 [1] Bioconductor devtools * 2.3.2 2020-09-18 [1] CRAN (R 4.0.2) digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0) DO.db 2.9 2020-06-08 [1] Bioconductor DOSE 3.14.0 2020-04-27 [1] Bioconductor downloader 0.4 2015-07-09 [1] CRAN (R 4.0.0) dplyr * 1.0.2 2020-08-18 [1] CRAN (R 4.0.2) e1071 1.7-3 2019-11-26 [1] CRAN (R 4.0.0) edgeR * 3.30.3 2020-06-02 [1] Bioconductor ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0) enrichplot 1.8.1 2020-04-29 [1] Bioconductor europepmc 0.4 2020-05-31 [1] CRAN (R 4.0.0) evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0) fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0) farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.0) fastmatch 1.1-0 2017-01-28 [1] CRAN (R 4.0.0) fgsea 1.14.0 2020-04-27 [1] Bioconductor forcats * 0.5.0 2020-03-01 [1] CRAN (R 4.0.0) foreach 1.5.0 2020-03-30 [1] CRAN (R 4.0.0) foreign 0.8-80 2020-05-24 [1] CRAN (R 4.0.2) fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) generics 0.0.2 2018-11-29 [1] CRAN (R 4.0.0) GenomeInfoDb 1.24.2 2020-06-15 [1] Bioconductor GenomeInfoDbData 1.2.3 2020-05-19 [1] Bioconductor GenomicAlignments 1.24.0 2020-04-27 [1] Bioconductor GenomicRanges 1.40.0 2020-04-27 [1] Bioconductor geosphere * 1.5-10 2019-05-26 [1] CRAN (R 4.0.2) ggcorrplot * 0.1.3 2019-05-19 [1] CRAN (R 4.0.2) ggforce 0.3.2 2020-06-23 [1] CRAN (R 4.0.2) ggplot.multistats 1.0.0 2019-10-28 [1] CRAN (R 4.0.0) ggplot2 * 3.3.2 2020-06-19 [1] CRAN (R 4.0.2) ggplotify * 0.0.5 2020-03-12 [1] CRAN (R 4.0.0) ggpmisc * 0.3.6 2020-09-07 [1] CRAN (R 4.0.2) ggpubr * 0.4.0 2020-06-27 [1] CRAN (R 4.0.2) ggraph 2.0.3 2020-05-20 [1] CRAN (R 4.0.0) ggrepel * 0.8.2 2020-03-08 [1] CRAN (R 4.0.0) ggridges 0.5.2 2020-01-12 [1] CRAN (R 4.0.0) ggsignif 0.6.0 2019-08-08 [1] CRAN (R 4.0.0) ggthemes * 4.2.0 2019-05-13 [1] CRAN (R 4.0.0) glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2) GO.db 3.11.4 2020-06-08 [1] Bioconductor goftest 1.2-2 2019-12-02 [1] CRAN (R 4.0.2) GOSemSim 2.14.1 2020-07-25 [1] Bioconductor gower 0.2.2 2020-06-23 [1] CRAN (R 4.0.2) graphlayouts 0.7.0 2020-04-25 [1] CRAN (R 4.0.0) gridExtra * 2.3 2017-09-09 [1] CRAN (R 4.0.0) gridGraphics 0.5-0 2020-02-25 [1] CRAN (R 4.0.0) gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.0) handlr * 0.3.0 2020-10-15 [1] CRAN (R 4.0.2) haven 2.3.1 2020-06-01 [1] CRAN (R 4.0.0) hexbin 1.28.1 2020-02-03 [1] CRAN (R 4.0.0) highr 0.8 2019-03-20 [1] CRAN (R 4.0.0) hms 0.5.3 2020-01-08 [1] CRAN (R 4.0.0) htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.2) htmlwidgets 1.5.1 2019-10-08 [1] CRAN (R 4.0.0) httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.0.0) httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2) igraph 1.2.5 2020-03-19 [1] CRAN (R 4.0.0) ipred 0.9-9 2019-04-28 [1] CRAN (R 4.0.0) IRanges * 2.22.2 2020-05-21 [1] Bioconductor irlba 2.3.3 2019-02-05 [1] CRAN (R 4.0.0) iterators 1.0.12 2019-07-26 [1] CRAN (R 4.0.0) jsonlite 1.7.1 2020-09-07 [1] CRAN (R 4.0.2) kableExtra * 1.2.1 2020-08-27 [1] CRAN (R 4.0.2) knitr * 1.29 2020-06-23 [1] CRAN (R 4.0.2) knn.covertree 1.0 2019-10-28 [1] CRAN (R 4.0.0) labeling 0.3 2014-08-23 [1] CRAN (R 4.0.0) laeken 0.5.1 2020-02-05 [1] CRAN (R 4.0.0) lattice * 0.20-41 2020-04-02 [1] CRAN (R 4.0.2) lava 1.6.7 2020-03-05 [1] CRAN (R 4.0.0) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.0.0) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.0) limma * 3.44.3 2020-06-12 [1] Bioconductor lmtest 0.9-38 2020-09-09 [1] CRAN (R 4.0.2) locfit 1.5-9.4 2020-03-25 [1] CRAN (R 4.0.0) lubridate 1.7.9 2020-06-08 [1] CRAN (R 4.0.2) magrittr * 1.5 2014-11-22 [1] CRAN (R 4.0.0) MASS 7.3-53 2020-09-09 [1] CRAN (R 4.0.2) Matrix 1.2-18 2019-11-27 [1] CRAN (R 4.0.2) matrixStats 0.56.0 2020-03-13 [1] CRAN (R 4.0.0) memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0) mgcv 1.8-33 2020-08-27 [1] CRAN (R 4.0.2) microbiome * 1.10.0 2020-04-27 [1] Bioconductor microbiomeutilities * 1.00.00 2020-10-02 [1] Github (microsud/microbiomeutilities@e17fff4) mime 0.9 2020-02-04 [1] CRAN (R 4.0.0) ModelMetrics 1.2.2.2 2020-03-17 [1] CRAN (R 4.0.0) modelr 0.1.8 2020-05-19 [1] CRAN (R 4.0.0) multtest 2.44.0 2020-04-27 [1] Bioconductor munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.0) nlme * 3.1-149 2020-08-23 [1] CRAN (R 4.0.2) nnet 7.3-14 2020-04-26 [1] CRAN (R 4.0.2) openxlsx 4.1.5 2020-05-06 [1] CRAN (R 4.0.2) org.Sc.sgd.db * 3.11.4 2020-06-13 [1] Bioconductor parallelDist * 0.2.4 2018-12-12 [1] CRAN (R 4.0.2) patchwork * 1.0.1 2020-06-22 [1] CRAN (R 4.0.0) pcaMethods 1.80.0 2020-04-27 [1] Bioconductor permute * 0.9-5 2019-03-12 [1] CRAN (R 4.0.0) pheatmap 1.0.12 2019-01-04 [1] CRAN (R 4.0.0) phyloseq * 1.32.0 2020-04-27 [1] Bioconductor pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.2) pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0) pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0) plotly * 4.9.2.1 2020-04-04 [1] CRAN (R 4.0.0) plyr 1.8.6 2020-03-03 [1] CRAN (R 4.0.0) polyclip 1.10-0 2019-03-14 [1] CRAN (R 4.0.0) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0) pROC 1.16.2 2020-03-19 [1] CRAN (R 4.0.0) processx 3.4.4 2020-09-03 [1] CRAN (R 4.0.2) prodlim 2019.11.13 2019-11-17 [1] CRAN (R 4.0.0) progress 1.2.2 2019-05-16 [1] CRAN (R 4.0.0) proxy 0.4-24 2020-04-25 [1] CRAN (R 4.0.0) ps 1.3.4 2020-08-11 [1] CRAN (R 4.0.2) purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.0.0) qvalue 2.20.0 2020-04-27 [1] Bioconductor R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0) ranger * 0.12.1 2020-01-10 [1] CRAN (R 4.0.0) rbibutils * 1.3 2020-10-05 [1] CRAN (R 4.0.2) RColorBrewer * 1.1-2 2014-12-07 [1] CRAN (R 4.0.0) Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2) RcppEigen 0.3.3.7.0 2019-11-16 [1] CRAN (R 4.0.0) RcppHNSW 0.3.0 2020-09-06 [1] CRAN (R 4.0.2) RcppParallel 5.0.2 2020-06-24 [1] CRAN (R 4.0.2) RCurl 1.98-1.2 2020-04-18 [1] CRAN (R 4.0.0) readr * 1.3.1 2018-12-21 [1] CRAN (R 4.0.0) readxl * 1.3.1 2019-03-13 [1] CRAN (R 4.0.0) recipes 0.1.13 2020-06-23 [1] CRAN (R 4.0.2) remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2) rentrez * 1.2.2 2019-05-02 [1] CRAN (R 4.0.2) reprex 0.3.0 2019-05-16 [1] CRAN (R 4.0.0) reshape2 * 1.4.4 2020-04-09 [1] CRAN (R 4.0.0) rhdf5 2.32.2 2020-07-03 [1] Bioconductor Rhdf5lib 1.10.1 2020-07-09 [1] Bioconductor rio 0.5.16 2018-11-26 [1] CRAN (R 4.0.0) rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.2) rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.2) robustbase 0.93-6 2020-03-23 [1] CRAN (R 4.0.0) rpart * 4.1-15 2019-04-12 [1] CRAN (R 4.0.2) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0) Rsamtools 2.4.0 2020-04-27 [1] Bioconductor RSpectra 0.16-0 2019-12-01 [1] CRAN (R 4.0.0) RSQLite 2.2.0 2020-01-07 [1] CRAN (R 4.0.0) rstatix 0.6.0 2020-06-18 [1] CRAN (R 4.0.2) rstudioapi 0.11 2020-02-07 [1] CRAN (R 4.0.0) rtracklayer 1.48.0 2020-04-27 [1] Bioconductor Rtsne 0.15 2018-11-10 [1] CRAN (R 4.0.0) rvcheck 0.1.8 2020-03-01 [1] CRAN (R 4.0.0) rvest 0.3.6 2020-07-25 [1] CRAN (R 4.0.2) S4Vectors * 0.26.1 2020-05-16 [1] Bioconductor scales * 1.1.1 2020-05-11 [1] CRAN (R 4.0.0) scatterpie 0.1.5 2020-09-09 [1] CRAN (R 4.0.2) scatterplot3d 0.3-41 2018-03-14 [1] CRAN (R 4.0.0) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0) SingleCellExperiment 1.10.1 2020-04-28 [1] Bioconductor smoother 1.1 2015-04-16 [1] CRAN (R 4.0.0) sp 1.4-2 2020-05-20 [1] CRAN (R 4.0.0) spatstat * 1.64-1 2020-05-12 [1] CRAN (R 4.0.2) spatstat.data * 1.4-3 2020-01-26 [1] CRAN (R 4.0.2) spatstat.utils 1.17-0 2020-02-07 [1] CRAN (R 4.0.2) stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2) stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.0) SummarizedExperiment 1.18.2 2020-07-14 [1] Bioconductor survival 3.2-3 2020-06-13 [1] CRAN (R 4.0.2) tensor 1.5 2012-05-05 [1] CRAN (R 4.0.2) testthat * 2.3.2 2020-03-02 [1] CRAN (R 4.0.0) tibble * 3.0.3 2020-07-10 [1] CRAN (R 4.0.2) tidygraph 1.2.0 2020-05-12 [1] CRAN (R 4.0.0) tidyr * 1.1.2 2020-08-27 [1] CRAN (R 4.0.2) tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.0) tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 4.0.0) timeDate 3043.102 2018-02-21 [1] CRAN (R 4.0.0) triebeard 0.3.0 2016-08-04 [1] CRAN (R 4.0.0) TTR 0.24.2 2020-09-01 [1] CRAN (R 4.0.2) tweenr 1.0.1 2018-12-14 [1] CRAN (R 4.0.0) UpSetR * 1.4.0 2019-05-22 [1] CRAN (R 4.0.0) urltools 1.7.3 2019-04-14 [1] CRAN (R 4.0.0) usethis * 1.6.3 2020-09-17 [1] CRAN (R 4.0.2) utf8 1.1.4 2018-05-24 [1] CRAN (R 4.0.0) vcd 1.4-7 2020-04-02 [1] CRAN (R 4.0.0) vctrs 0.3.4 2020-08-29 [1] CRAN (R 4.0.2) vegan * 2.5-6 2019-09-01 [1] CRAN (R 4.0.0) VIM 6.0.0 2020-05-08 [1] CRAN (R 4.0.0) viridis * 0.5.1 2018-03-29 [1] CRAN (R 4.0.0) viridisLite * 0.3.0 2018-02-01 [1] CRAN (R 4.0.0) webshot 0.5.2 2019-11-22 [1] CRAN (R 4.0.0) withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0) xfun 0.17 2020-09-09 [1] CRAN (R 4.0.2) XML 3.99-0.5 2020-07-23 [1] CRAN (R 4.0.2) xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.0) xts 0.12.1 2020-09-09 [1] CRAN (R 4.0.2) XVector 0.28.0 2020-04-27 [1] Bioconductor yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0) zip 2.1.1 2020-08-27 [1] CRAN (R 4.0.2) zlibbioc 1.34.0 2020-04-27 [1] Bioconductor zoo 1.8-8 2020-05-02 [1] CRAN (R 4.0.0) [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library ```
sckott commented 3 years ago

thanks @taylorreiter for the question

If there's more than 1 citation we use lapply, see https://github.com/ropensci/handlr/blob/master/R/bibtex_writer.R#L36

Problem with that is it returns a list, but a list is best to keep each citation distinct from one another. That is, we could output a character vector but there's no way then for the user to easily separate citations if they wanted to inside of R. A character vector would be easier to then write to disk, but I think the below example is easy enough. What do you think?

library(handlr)
tmp <- citeproc_reader("~/Downloads/references.json.txt")
x <- bibtex_writer(tmp)
lapply(x, cat, file="test.bib", sep="\n", append=TRUE)

an alternative is using the R6 class like:

library(handlr)
x <- HandlrClient$new("~/Downloads/references.json.txt")
x$read()
x$write("bibtex", file="test4.bib")

which does handle multiple citations automatically for you

taylorreiter commented 3 years ago

Thank you @sckott!! These are wonderful examples.