-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessRequest.R
148 lines (111 loc) · 3.95 KB
/
ProcessRequest.R
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
source ("basicFunctions.R")
source ("auth.R")
library(mailR)
# code <- list.files(file.path("output"))
code <- args[1]
#Process ----------
request <- readRDS(file.path("output", code, "request.rds"))
email <- request$email
dirname <- request$dirname
term <- request$term
retmax <- 10000
year <- request$year
force <- request$force
nAuthors <- 100
#Extract forced authors ------
if(force[1] != ""){
force <- strsplit(force, ",")[[1]]
for(i in 1:length(force)){
force[i] <- trimws(force[i])
}
}
#Get PUBMED results ------
search_results <- makeQuery(term, year, retmax)
if(length(search_results)==0){
filename <- paste(dirname, "/results.rds", sep="")
toSave <- list(ter = term)
saveRDS(toSave, file = filename)
myCodeErrorMail(term, code, email)
stop("Could not get results from query")
}
if(force[1] != ""){
for(i in 1:length(force)){
termp <- paste(term, "AND", force[i], sep=" ")
search_results <- c(search_results, makeQuery(termp, year, retmax))
}
}
search_results <- unique(search_results)
files <- fetchSearchResults(search_results)
#Process PUBMED files --------
if(file.exists(file.path("output", code, "df.rds"))){
df <- readRDS(file.path("output", code, "df.rds"))
} else {
df <- extractInfo(files)
saveRDS(df, file.path("output", code, "df.rds"))
}
dataFrame <- df[["df"]]
#Get authors to display --------
freqAuthors <- data.frame(table(c(dataFrame[,"FirstAuthor"], dataFrame[, "LastAuthor"])))
freqAuthors <- freqAuthors[order(freqAuthors[,"Freq"], decreasing = T),]
if(!(is.na(force[1]) || force[1]=="")){
forcedAuthors <- NULL
for(i in 1:length(force)){
forceEntry <- strsplit(force[i], " ")[[1]]
forcedAuthorsFirst <- grep(forceEntry[1], freqAuthors[,"Var1"])
forcedAuthorsLast <- grep(forceEntry[length(forceEntry)], freqAuthors[,"Var1"])
forcedAuthors <- c(forcedAuthors, intersect(forcedAuthorsFirst, forcedAuthorsLast))
}
toKeep <- c(1:(nAuthors-length(forcedAuthors)))
toKeep <- toKeep[-forcedAuthors]
toKeep <- c(forcedAuthors, toKeep)
} else {
toKeep <- 1:nAuthors
}
mainAuthors <- as.character(freqAuthors[toKeep, "Var1"])
# Make workdclouds ------
figs <- createWordClouds(df[["df"]], mainAuthors, nkeys=30)
#Get faces and make wordcloud images-------
imgs <- list()
library(rvest)
dirname <- file.path("www", code)
if(!dir.exists(dirname)){
dir.create(dirname)
}
for (i in 1:length(figs)){
#Make a google search and extract the first image
sterm <- paste(term, names(figs[i]), "profile")
sterm <- gsub(" ", "+", sterm)
url <- paste("https://www.google.com/search?q=", sterm, "&tbm=isch", sep="")
page <- read_html(url)
img <- page %>% html_nodes("img") %>% html_attr("src")
#img <- "" #THIS SHOULD BE REMOVED IF img IS RUN
imgs <- c(imgs, list(img[[2]]))
names(imgs)[i] <- make.names(names(figs[i]))
# saveWidget(figs[[i]], paste(dirname, "/tmp.html", sep=""), selfcontained = F)
# webshot(paste(dirname, "/tmp.html", sep=""),
# paste(dirname,"/", names(imgs)[i], ".jpg", sep=""),
# delay = 3, vwidth = 300, vheight=300) # changed to png.
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#Network -----------------
Authors <- as.character(freqAuthors[, "Var1"])
if(!(is.na(force[1]) || force[1]=="")){
net <- MakeNetwork(df[["df"]], Authors = Authors,
mainAuthors = mainAuthors,
forcedAuthors = freqAuthors[forcedAuthors, "Var1"],
collaborators = df[["collaborators"]])
} else {
net <- MakeNetwork(df[["df"]], Authors = Authors,
mainAuthors = mainAuthors,
collaborators = df[["collaborators"]])
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#Save Results -----------------
dirname <- file.path(getwd(), "output", code)
filename <- paste(dirname, "/results.rds", sep="")
toSave <- list(ter = term, df = df, figs = figs, imgs = imgs, net = net)
saveRDS(toSave, file = filename)
myCodeMail(term, code, email)
#}