Search code examples
rcurlhttrrcurl

Putting a curl command into a R script and capturing the output to a variable


I would need to use the following bash command (or equivalent) into a R script. ideally, I would need to capture the output into a variable

curl -LH "Accept: text/bibliography; style=bibtex" https://doi.org/10.1002/cbin.11105

I checked the httr library, but I had no luck. Also, I tried to embed the command into a system call, but I was getting a hard to parse output using the option "intern = TRUE". Thanks in advance


Solution

  • library(httr)
    res <- GET("https://doi.org/10.1002/cbin.11105", accept("text/bibliography; style=bibtex"))
    res
    # Response [https://api.crossref.org/v1/works/10.1002%2Fcbin.11105/transform]
    #   Date: 2022-03-18 19:21
    #   Status: 200
    #   Content-Type: text/bibliography
    #   Size: 475 B
    #  @article{2019, title={Silencing of MEG3 inhibited ox-LDL-induced inflammation and apoptosis in macrophages via ...
    

    (Note the Status: 200 to ensure it was successful.)

    content(res)
    # No encoding supplied: defaulting to UTF-8.
    # [1] " @article{2019, title={Silencing of MEG3 inhibited ox-LDL-induced inflammation and apoptosis in macrophages via modulation of the MEG3/miR-204/CDKN2A regulatory axis}, volume={43}, ISSN={1095-8355}, url={http://dx.doi.org/10.1002/cbin.11105}, DOI={10.1002/cbin.11105}, number={4}, journal={Cell Biology International}, publisher={Wiley}, author={Yan, Long and Liu, Zhanchuan and Yin, Haoyuan and Guo, Zhenjie and Luo, Qi}, year={2019}, month={Feb}, pages={409?420} }\n"