-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
305 lines (283 loc) · 14.5 KB
/
app.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# ScholarlyOutput
# A shiny app to visualise a GoogleScholar profile
# https://github.com/JDLeongomez/ScholarlyOutput
# https://zenodo.org/badge/latestdoi/536271372
# Juan David Leongómez - https://jdleongomez.info/
library(shiny)
library(thematic)
library(shinythemes)
library(shinycssloaders)
library(shinyWidgets)
library(colourpicker)
library(stringr)
library(scholar)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggpubr)
library(scales)
library(purrr)
# Define UI for application that draws a histogram
ui <- fluidPage(theme = c("united"),
# Application title
titlePanel(title =
tags$link(rel = "icon", type = "image/gif", href = "img/icon.png"),
"ScholarlyOutput"),
tags$h1(HTML("<a style=color:#EA4335; href='https://github.com/JDLeongomez/ScholarlyOutput'><b><i>ScholarlyOutput</b></i></a>")),
tags$h4(HTML("Plot your scholarly output from <img src='https://upload.wikimedia.org/wikipedia/commons/2/28/Google_Scholar_logo.png' width='150'>")),
tags$h6(HTML("App created in <a style=color:#EA4335; href='https://shiny.rstudio.com/'>Shiny</a> by
<a style=color:#EA4335; href='https://jdleongomez.info/es/'>Juan David Leongómez</a>
· 2023 <br>
Code available on
<a style=color:#EA4335; href='https://github.com/JDLeongomez/ScholarlyOutput'>GitHub</a> ·
<a href='https://shiny.jdl-svr.lat/ScholarlyOutput_ES/'>Versión en Español</a>")),
tags$h6(HTML("<p dir='auto'><a target='_blank' rel='noopener noreferrer nofollow' href='https://camo.githubusercontent.com/aafa45b848b5c22e83ab7d8f3c6e5762e995ea8ee98f0395d08ce82cf2ad9a76/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f4a444c656f6e676f6d657a2f53636f6c61726c794f7574707574'><img src='https://camo.githubusercontent.com/aafa45b848b5c22e83ab7d8f3c6e5762e995ea8ee98f0395d08ce82cf2ad9a76/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f4a444c656f6e676f6d657a2f53636f6c61726c794f7574707574' alt='' data-canonical-src='https://img.shields.io/github/last-commit/JDLeongomez/ScholarlyOutput' style='max-width: 100%;'></a>
<a href='https://github.com/JDLeongomez/ScholarlyOutput_EN/blob/main/LICENSE'>GNU General Public License v3.0</a> |
<a href='https://zenodo.org/badge/latestdoi/536271372' rel='nofollow'>DOI</a>"
)),
# Sidebar with a slider input for accent colour
fluidRow(
column(3,
hr(),
p(HTML("This Shiny app gets publucations and citation information from
<a style=color:#EA4335; href='https://scholar.google.com/'>Google Scholar</a>
using the
<a style=color:#EA4335; href='https://cran.r-project.org/web/packages/scholar/vignettes/scholar.html'>scholar</a>
R package, and plots both the citations per publication (including <i>h</i>-
and <i>g</i>-index; panel <b>A</b>), as well as the number of publications
and citations per year (including total number of citations; panel <b>B</b>).")),
hr(),
tags$h4("Profile to plot"),
textInput("profl",
"Please copy and paste your full Google Scholar profile URL:",
value = "https://scholar.google.com/citations?user=8Q0jKHsAAAAJ",
width = 600,
placeholder = "https://scholar.google.com/citations?user=8Q0jKHsAAAAJ"),
h4("Save the Plot"),
downloadButton("SavePlotPNG", label = "PNG"),
downloadButton("SavePlotPDF", label = "PDF"),
downloadButton("SavePlotSVG", label = "SVG"),
hr(),
tags$h4("Graphical options"),
colourInput("accentCol",
"Accent colour (click to select):",
"#EA4335",
returnName = TRUE),
tags$h6(HTML("<b>Note:</b> alternatively, you can paste the
name (e.g. <i><b>blue</b></i>) or
<a style=color:#EA4335; href='https://www.google.com/search?q=hex+color+picker' target='_blank'>HEX value</a>
(e.g. <b>#008080</b>) of a colour")),
hr(),
tags$h4("Filter publications"),
tags$h6(HTML("I recommend doing some
<a style=color:#EA4335; href='https://scholar.google.com/intl/es/scholar/citations.html#setup'>maintenance</a>
of your profile before creating this plot. This may include, for example,
merging duplicates and making sure that all relevant information, including
year, is complete and accurate. <br><br>
Publications without date are automatically excluded from plots (but not
from the total citation count). However, because the quality of the plot
will be limited by the quality of the data, I have added an option to exclude
publications reported as published before a certain year.")),
numericInput("minyear",
"Exclude publications dated before:",
value = 1900,
min = 1,
max = lubridate::year(Sys.Date()),
width = 200),
br(),
br(),
br(),
#downloadLink("downloadPlot", "Download Plot")
),
# Show a plot of the generated distribution
column(6,
offset = 1,
nextGenShinyApps::submitButton("runSim", text = "All set? Make your plot!",
icon("paper-plane"), bg.type = "danger"),
br(),
br(),
plotOutput("scholarPlot") %>%
withSpinner(color = "#EA4335")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$scholarPlot <- renderPlot(
width = 1200,
height = 600,
res = 120,
{
#Define Scholar profile
pfl <- input$profl |>
str_split(pattern = 'user\\=') |>
map_chr(c(2)) |>
str_sub(start = 1, end = 12)
#Get data from Scholar (filtering specific non-academic publications)
##Publications
pubs <- get_publications(pfl) |>
filter(!(journal == "" | journal == "target")) |>
filter(!(year == "" | year < input$minyear))
##Citations
ct <- get_citation_history(pfl)
##Full profile
profile <- get_profile(pfl)
#Create data frame
##Define years (from year of first publication to current year)
years <- data.frame(year = c(min(pubs$year, na.rm = TRUE):as.numeric(format(Sys.Date(),'%Y'))))
##Get number of publications per year
pd <- pubs |>
group_by(year) |>
summarise(pt = length(year)) |>
drop_na(year)
##Merge years and number of publications per year
pt <- years |>
full_join(pd) |>
arrange(year)
##Add number of citations per year
dat <- pt |>
full_join(ct) |>
arrange(year) |>
mutate(year = as.integer(year)) |>
mutate(across(everything(), ~replace_na(.x, 0)))
#Calculate metrics
##Get year to count last three years
yearRecent <- as.integer(format(Sys.Date(), '%Y')) - 2
##Total number of citations
citSum <- profile$total_cites
##Recent citations (last three years)
citRecentSum <- ct |>
summarize(sumB = sum(cites[year >= yearRecent]))
##Number of publications with more than 50 citations
count50cit <- nrow(ct[ct$cites > 50, ])
##Proportion of citation in the last three years
citRecentProp <- citRecentSum/citSum
#g-index and h-index
##g-index
pubs$square <- as.numeric(row.names(pubs))^2
pubs$sums <- cumsum(pubs$cites)
g_index <- max(which(pubs$square < pubs$sums))
##h-index
h_index <- profile$h_index
##Rank publications according to number of citations
pubs$rank <- seq.int(nrow(pubs))
##Squared root of cumulative citations (rounded down)
pubs$sqr <- floor(sqrt(pubs$sums))
##Define parameters for secondary axis
ylim.prim <- c(0, max(dat$pt)*1.25) # publications
ylim.sec <- c(0, max(dat$cites)) # citations
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
## Define colors
colors <- c("Citations per publication" = "black", "Square root of cumulative\ncitations (rounded down)" = "grey")
#Plot 1: Citations per publication, h-index and g-index
p1 <- ggplot(pubs, aes(x = rank, y = cites)) +
geom_abline(intercept = 0, slope = 1, color = input$accentCol, linetype = "dotted", linewidth = 0.7) +
geom_line(aes(color = "Citations per publication")) +
geom_line(aes(y = floor(sqrt(sums)), color = "Square root of cumulative\ncitations (rounded down)")) +
scale_color_manual(values = colors) +
geom_segment(aes(x = h_index, y = h_index, xend = h_index, yend = h_index+(g_index*0.5)),
size = 0.1, color = input$accentCol,
arrow = arrow(length = unit(0.3, "cm"), type = "closed")) +
geom_segment(aes(x = g_index, y = g_index, xend = g_index, yend = g_index*1.5),
size = 0.1, color = input$accentCol,
arrow = arrow(length = unit(0.3, "cm"), type = "closed")) +
annotate("text", y = h_index+(g_index*0.55), x = h_index,
label= bquote(italic(h)*'-'*index == .(h_index)),
hjust = 0, angle = 90,
color = input$accentCol, size = 3) +
annotate("text", y = g_index*1.55, x = g_index,
label = bquote(italic(g)*'-'*index == .(g_index)),
hjust = 0, angle = 90,
color = input$accentCol, size = 3) +
annotate("point", x = h_index, y = h_index,
color = input$accentCol) +
annotate("point", x = g_index, y = g_index,
color = input$accentCol) +
labs(x = "Publication (citation rank)",
y = "Citations",
subtitle = expression(paste("Citations per publication, ", italic(~h), "-index and", italic(~g), "-index"))) +
theme_pubclean() +
theme(axis.line.x = element_line(color = "grey"),
axis.ticks.x = element_line(color = "grey"),
axis.line.y.left = element_line(color = "black"),
axis.ticks.y.left = element_line(color = "black"),
axis.text.y.left = element_text(color = "black"),
axis.title.y.left = element_text(color = "black"),
legend.justification = c(1,1),
legend.position = c(1,1),
legend.title = element_blank(),
legend.key = element_rect(fill = "transparent", colour = "transparent"),
plot.subtitle = element_text(size = 9),
axis.text = element_text(size = 6),
axis.title = element_text(size = 8))
#Plot2: Publications and citations per year
##Plot
p2 <- ggplot(dat, aes(year, pt)) +
geom_col(fill = "lightgrey") +
geom_line(aes(y = a + cites*b), color = input$accentCol) +
scale_x_continuous(breaks = pretty_breaks()) +
scale_y_continuous("Publications", breaks = pretty_breaks(), sec.axis = sec_axis(~ (. - a)/b, name = "Citations")) +
theme_pubclean() +
annotate("text", y = Inf, x = -Inf,
label = paste0("Total citations = ", comma(profile$total_cites)),
vjust = 3, hjust = -0.1,
color = input$accentCol, size = 3) +
theme(axis.line.x = element_line(color = "grey"),
axis.ticks.x = element_line(color = "grey"),
axis.line.y.right = element_line(color = input$accentCol),
axis.ticks.y.right = element_line(color = input$accentCol),
axis.text.y.right = element_text(color = input$accentCol),
axis.title.y.right = element_text(color = input$accentCol),
axis.line.y.left = element_line(color = "black"),
axis.ticks.y.left = element_line(color = "black"),
axis.text.y.left = element_text(color = "black"),
axis.title.y.left = element_text(color = "black"),
plot.subtitle = element_text(size=9),
axis.text = element_text(size = 6),
axis.title = element_text(size = 8)) +
labs(x = "Year",
subtitle = "Publications and citations per year")
#Final plot
p.fin <- ggarrange(p1, p2,
ncol = 2,
labels = "AUTO")
##Add date to final plot
Sys.setlocale("LC_TIME", "C")
annotate_figure(p.fin,
bottom = text_grob(paste0("Data from Google Scholar. Plot updated ",
format(Sys.Date(),'%B %d, %Y')),
hjust = 1.05, x = 1, size = 8),
top = text_grob(profile$name,
face = "bold", hjust = -0.1, x = 0, size = 14))
})
output$SavePlotPNG <- downloadHandler(
filename = function(file) {
"Scholar_profile.png"
#ifelse(is.null(input$DataFile), return(), str_c(input$Title, ".png"))
},
content = function(file) {
ggsave(file, width = 2400, height = 1200, units = "px", dpi = 300, device = "png")
}
)
output$SavePlotPDF <- downloadHandler(
filename = function(file) {
"Scholar_profile.pdf"
#ifelse(is.null(input$DataFile), return(), str_c(input$Title, ".png"))
},
content = function(file) {
ggsave(file, width = 2400, height = 1200, units = "px", dpi = 300, device = "pdf")
}
)
output$SavePlotSVG <- downloadHandler(
filename = function(file) {
"Scholar_profile.svg"
#ifelse(is.null(input$DataFile), return(), str_c(input$Title, ".png"))
},
content = function(file) {
ggsave(file, width = 2400, height = 1200, units = "px", dpi = 300, device = "svg")
}
)
}
# Run the application
shinyApp(ui = ui, server = server)