Skip to content

Commit e24cf1c

Browse files
committed
Update ScienceDirect in R recipe
1 parent 7ab29a3 commit e24cf1c

1 file changed

Lines changed: 65 additions & 23 deletions

File tree

src/r/sdirect.md

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,72 @@
1+
---
2+
title: "ScienceDirect API in R"
3+
output:
4+
html_document:
5+
keep_md: true
6+
---
7+
18
# ScienceDirect API in R
29

310
by Michael T. Moen
411

5-
These recipe examples demonstrate how to use Elsevier’s [ScienceDirect API](https://dev.elsevier.com/) to retrieve full-text articles in various formats (XML, text).
12+
These recipe examples demonstrate how to use Elsevier’s <a href="https://dev.elsevier.com/" target="_blank">ScienceDirect API</a> to retrieve full-text articles in various formats (XML, text).
613

714
*This tutorial content is intended to help facilitate academic research. Please check your institution for their Text and Data Mining or related License Agreement with Elsevier.*
815

9-
- **Documentation**
10-
- [ScienceDirect API](https://dev.elsevier.com/)
11-
- [ScienceDirect API Documentation](https://dev.elsevier.com/sd_api_spec.html)
12-
13-
- **Terms**
14-
- [ScienceDirect API Terms of Use](https://dev.elsevier.com/api_key_settings.html)
16+
Please see the following resources for more information on API usage:
1517

16-
- **Data Reuse**
17-
- [Elsevier Text & Data Mining](https://dev.elsevier.com/tecdoc_text_mining.html)
18+
- Documentation
19+
- <a href="https://dev.elsevier.com/" target="_blank">ScienceDirect API</a>
20+
- <a href="https://dev.elsevier.com/sd_api_spec.html" target="_blank">ScienceDirect API Documentation</a>
21+
- Terms\n",
22+
- <a href="https://dev.elsevier.com/api_key_settings.html" target="_blank">ScienceDirect API Terms of Use</a>
23+
- Data Reuse
24+
- <a href="https://dev.elsevier.com/tecdoc_text_mining.html" target="_blank">Elsevier Text & Data Mining</a>
1825

19-
> **Note:** See your institution's rate limit in the [ScienceDirect API Terms of Use](https://dev.elsevier.com/api_key_settings.html).
26+
_**NOTE:**_ See your institution's rate limit with <a href="https://dev.elsevier.com/api_key_settings.html" target="_blank">ScienceDirect API Terms of Use</a>.
2027

28+
*If you have copyright or other related text and data mining questions, please contact The University of Alabama Libraries or your respective library/institution.*
2129

22-
*These recipe examples were tested on February 7, 2025.*
30+
*These recipe examples were tested on October 27, 2025.*
2331

2432
## Setup
2533

2634
### Import Libraries
2735

28-
```r
36+
The following packages need to be installed into your environment to run the code examples in this tutorial. These packages can be installed with `install.packages()`.
37+
38+
- <a href="https://cran.r-project.org/web/packages/httr/index.html" target="_blank">httr: Tools for Working with URLs and HTTP</a>
39+
40+
We load the libraries used in this tutorial below:
41+
42+
43+
``` r
2944
library(httr)
3045
```
3146

3247
### Import API Key
3348

34-
An API key is required to access the ScienceDirect API. Registration is available on the [Elsevier developer portal](https://dev.elsevier.com/). The key is imported from an environment variable below:
49+
An API key is required for to access the ScienceDirect API. You can sign up for one at <a href="https://dev.elsevier.com/" target="_blank">Elsevier developer portal</a>.
50+
51+
We keep our token in a `.Renviron` file that is stored in the working directory and use `Sys.getenv()` to access it. The `.Renviron` should have an entry like the one below.
3552

36-
```r
37-
myAPIKey <- Sys.getenv("sciencedirect_key")
53+
```text
54+
SCIENCE_DIRECT_API_KEY="PUT_YOUR_API_KEY_HERE"
55+
```
56+
57+
Below, we can test to whether the key was successfully imported.
58+
59+
60+
``` r
61+
if (nzchar(Sys.getenv("SCIENCE_DIRECT_API_KEY"))) {
62+
print("API key successfully loaded.")
63+
} else {
64+
warning("API key not found or is empty.")
65+
}
66+
```
67+
68+
```
69+
## [1] "API key successfully loaded."
3870
```
3971

4072
### Identifier Note
@@ -43,41 +75,51 @@ We will use DOIs as the article identifiers. See our Crossref and Scopus API tut
4375

4476
## 1. Retrieve full-text XML of an article
4577

46-
```r
78+
79+
``` r
4780
# For XML download
4881
elsevier_url <- "https://api.elsevier.com/content/article/doi/"
4982
doi1 <- '10.1016/j.tetlet.2017.07.080' # Example Tetrahedron Letters article
50-
fulltext1 <- GET(paste0(elsevier_url, doi1, "?APIKey=", myAPIKey, "&httpAccept=text/xml"))
83+
fulltext1 <- GET(paste0(
84+
elsevier_url, doi1,
85+
"?APIKey=", Sys.getenv("SCIENCE_DIRECT_API_KEY"),
86+
"&httpAccept=text/xml"))
5187

5288
# Save to file
5389
writeLines(content(fulltext1, "text"), "fulltext1.xml")
5490
```
5591

5692
## 2. Retrieve plain text of an article
5793

58-
```r
94+
95+
``` r
5996
# For simplified text download
6097
doi2 <- '10.1016/j.tetlet.2022.153680' # Example Tetrahedron Letters article
61-
fulltext2 <- GET(paste0(elsevier_url, doi2, "?APIKey=", myAPIKey, "&httpAccept=text/plain"))
98+
fulltext2 <- GET(paste0(
99+
elsevier_url, doi2,
100+
"?APIKey=", Sys.getenv("SCIENCE_DIRECT_API_KEY"),
101+
"&httpAccept=text/plain"))
62102

63103
# Save to file
64104
writeLines(content(fulltext2, "text"), "fulltext2.txt")
65105
```
66106

67107
## 3. Retrieve full-text in a loop
68108

69-
```r
109+
110+
``` r
70111
# Make a list of 5 DOIs for testing
71112
dois <- c('10.1016/j.tetlet.2018.10.031',
72113
'10.1016/j.tetlet.2018.10.033',
73114
'10.1016/j.tetlet.2018.10.034',
74115
'10.1016/j.tetlet.2018.10.038',
75116
'10.1016/j.tetlet.2018.10.041')
76-
```
77117

78-
```r
79118
for (doi in dois) {
80-
article <- GET(paste0(elsevier_url, doi, "?APIKey=", myAPIKey, "&httpAccept=text/plain"))
119+
article <- GET(paste0(
120+
elsevier_url, doi,
121+
"?APIKey=", Sys.getenv("SCIENCE_DIRECT_API_KEY"),
122+
"&httpAccept=text/plain"))
81123
doi_name <- gsub("/", "_", doi)
82124
writeLines(content(article, "text"), paste0(doi_name, "_plain_text.txt"))
83125
Sys.sleep(1)

0 commit comments

Comments
 (0)