-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathropensci_demo.Rmd
98 lines (71 loc) · 3.04 KB
/
ropensci_demo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
## rOpenSci Demo
[rOpenSci](http://ropensci.org/)
### Getting data from the literature - PLOS
This example demonstrates how you can easily get literature data from Public Library of Science from R.
#### Install rplos
```{r install, eval=FALSE}
install.packages(c("rplos","tm","ggplot2"), dependencies=TRUE)
```
#### Load rplos
```{r load}
library(rplos)
```
#### Search for mentions of Fisher in the author field, returning title and author fields, searching in full papers (not including figure captions, etc.), returning only 25 results.
```{r searchplos, message=FALSE, warning=FALSE}
key <- "key"
out <- searchplos(terms='author:"Fisher"', fields='title,author', toquery='doc_type:full', limit=25, key=key)
head(out) # first six rows
```
#### Quickly visualize variation in frequency of word usage in PLOS journals
A built in function in `rplos` called `plosword` makes a bar plot of frequency of terms across PLOS articles.
```{r plosword, message=FALSE, warning=FALSE}
library(ggplot2)
plosword(list('monkey','Helianthus','sunflower','protein','whale'), key=key, vis = TRUE)$plot +
theme_grey(base_size=18)
```
Or, we can do the same thing manually. First, install a few packages if you don't have them already.
```{r eval=FALSE}
install.packages(c("RCurl","RJSONIO"))
```
Then get counts of terms across papers, then plot using `ggplot2`
```{r visualize, message=FALSE, warning=FALSE}
library(RCurl)
library(RJSONIO)
search <- function(x) {
args <- list(q = x, fl = 'id', wt = 'json')
tt <- getForm("http://api.plos.org/search", .params = args)
fromJSON(I(tt))$response$numFound
}
terms <- c('monkey','Helianthus','sunflower','protein','whale')
temp <- sapply(terms, search, USE.NAMES=FALSE)
df <- data.frame(Term = terms, No_Articles = temp)
ggplot(df, aes(x=Term, y=No_Articles)) +
theme_grey(base_size=18) +
geom_bar(stat="identity")
```
#### Get abstracts of 500 papers, and use the tm package for text mining.
Get 500 abstracts from PLOS One only. The `*:*` is special syntax to denote *give back everything*
```{r getabstracts, message=FALSE, warning=FALSE}
out <- searchplos(terms='*:*', fields='abstract', toquery=list('cross_published_journal_key:PLoSONE', 'doc_type:full'), limit=500, key=key)
out$abstract[1:3] # take a peek
```
Load the tm package, and create a document library
```{r corpus, message=FALSE, warning=FALSE}
library(tm)
(corpus <- Corpus(VectorSource(out$abstract)))
```
Create a term-document matrix from the corpus, and inspect it.
```{r tdm, message=FALSE, warning=FALSE}
tdm <- DocumentTermMatrix(corpus,
control = list(removePunctuation = TRUE,
stopwords = TRUE,
removeNumbers = TRUE))
inspect(tdm[1:5,1:5])
```
Various operations on document term matrices
```{r freqterm, message=FALSE, warning=FALSE}
# find terms that occur at least five times across documents
findFreqTerms(tdm, 250)
# find associations (terms which correlate) with at least 0.3 correlation for the term "result"
findAssocs(tdm, "cells", 0.3)
```