-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
159 lines (141 loc) · 6.34 KB
/
server.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
library(shiny)
library(ggplot2)
library(WikidataR)
library(SPARQL)
source("mySPARQL.R") #until the SPARQL package is fixed for UTF-8
library(stringr)
library(tidyr)
library(dplyr)
library(ggthemes)
library(magrittr)
theme_set(theme_minimal(12))
source("queries.R") #to get the data from Wikidata
shinyServer(function(input, output, session) {
disable("download")
runjs("document.getElementById('prenom').focus()")
output$info<-renderUI({
switch(input$tabset,
"tabAnnees"=tags$p(strong("Dates"),"permet de choisir l'intervalle à afficher ;",
br(),strong("Années"), "donne la largeur des barres de l'histogramme ;",
br(),strong("Pays"),"permet de filtrer sur un ou plusieurs pays ; laisser vide pour tous les pays,
la liste qui s'affiche est classée dans l'ordre décroissant
du nombre de personnes ayant le prénom cherché dans ce pays.",
br(),em("Inconnu"),"indique que le pays n'est pas renseigné dans Wikidata."),
"tabPays"=tags$p(strong("Pays"),"permet de sélectionner le nombre de pays à afficher ;",br(),
em("Inconnu"),"indique que le pays n'est pas renseigné dans Wikidata."),
"tabNuageMetiers"=tags$p(em("Attention")," : certains mots trop longs peuvent ne pas s'afficher.",br(),
strong("Couper métiers"),"permet d'insérer un retour à la ligne si les mots contiennent un esapce ou tiret ;",
br(),strong("Fréquence minimum"), "les métiers appraissant moins fréquemment que ce paramètre ne seront pas affichés ;" ,
br(),strong("Echelle min"),"et",strong("Echelle max"), "permettent de régler la taille des mots, en fonction de leur fréquence.",
br(),em("Note :"),"par construction de la requête, ne sont récupérées que les informations quand le métier est précisé.
Une personne peut avoir plusieurs métiers."),
"tabMetiers"=tags$p(strong("Métiers"),"permet de sélectionner le nombre de métiers à afficher ;",br(),
em("Note :"),"par construction de la requête, les informations ne sont récupérées que lorsque le métier est précisé.
Une personne peut avoir plusieurs métiers.")
)
})
dataPrenom<-eventReactive(input$action,{
withProgress(value=0.2,message="Querying Wikidata",{
disable("download")
res<-queryStreamWithProgress(input$prenom)
minRes<-min(res$annee,na.rm=TRUE)
maxRes<-max(res$annee,na.rm=TRUE)
updateSliderInput(session,"dates",min=minRes,max=maxRes,
value=c(minRes,maxRes))
updateSelectizeInput(session,"pays",choices=c("Choisir un ou plusieurs"="",
names(sort(-table(res$pays)))))
updateSliderInput(session,"nbMetiers",max=length(levels(res$metier)),
value=10)
shinyjs::hide("pretitle")
enable("download")
res
})
})
output$title<-renderUI({
metier<- dataPrenom() %>% count(metier) %>% top_n(1,n) %>% use_series(metier)
metiers<-paste(metier,collapse="/")
pays<- dataPrenom() %>% count(pays) %>% top_n(1,n) %>% use_series(pays)
pays2<-paste(pays,collapse = "/")
tags$h2(paste("Dans Wikidata,",isolate(input$prenom),"est",metiers,"en",pays2))
})
output$download <- downloadHandler(
filename = function() {
paste(input$prenom, '.csv', sep='')
},
content = function(file) {
write.csv(dataPrenom(), file)
}
)
output$naissance<-renderPlot({
if(is.null(input$pays)){
newdata<-dataPrenom() %>%
distinct(item, .keep_all = TRUE)
}
else{
newdata<-dataPrenom() %>%
filter(pays %in% input$pays) %>%
distinct(item, .keep_all = TRUE)
}
minGraph<-floor(input$dates[1]/10)*10
maxGraph<-ceiling(input$dates[2]/10)*10
ggplot(newdata,aes(x=annee))+
geom_histogram(binwidth = input$regroup,aes(fill=pays))+
ggtitle(paste("Années de naissance des", isolate(input$prenom) ,
"dans Wikidata, regroupé par",input$regroup,"ans"))+
ylab("Nombre")+
xlab(NULL) +
scale_x_continuous(limits=c(minGraph,maxGraph),
breaks=seq(minGraph,maxGraph,
by=max(5,(maxGraph-minGraph)/10)))+
scale_fill_hue("Pays")+
theme(legend.position = "bottom")
})
output$metiers<-renderPlot({
everyOccupation<-dataPrenom() %>%
group_by(metier) %>%
count(metier)
if(input$cut){
everyOccupation$occ<-mapply(couperMot,everyOccupation$metier)}
else everyOccupation$occ<-everyOccupation$metier
set.seed(14)
wordcloud(everyOccupation$occ,everyOccupation$n,
scale=c(input$scaleMax,input$scaleMin),
min.freq=input$minFreq,
colors=brewer.pal(6,"Dark2"),
random.order = FALSE,rot.per=0.3)
})
output$histoMetier<-renderPlot({
met<-dataPrenom() %>%
count(metier) %>%
arrange(desc(n)) %>%
top_n(input$nbMetiers,n) %>%
droplevels()
met$metier <-reorder(met$metier,met$n,identity)
ggplot(data=met,aes(x=metier,y=n))+
geom_bar(stat="identity",fill="darkorange")+
scale_x_discrete(limits=levels(met$metier))+
geom_text(aes(label=n),hjust=1.5,colour="white")+
xlab(NULL)+
ylab(NULL)+
ggtitle(label = paste("Top",input$nbMetiers,"des métiers de",
isolate(input$prenom),"dans Wikidata"))+
coord_flip()
})
output$histoPays<-renderPlot({
topPays<-dataPrenom() %>%
count(pays) %>%
arrange(desc(n)) %>%
top_n(input$nbPays,n) %>%
droplevels()
topPays$pays <-reorder(topPays$pays,topPays$n,identity)
ggplot(data=topPays,aes(x=pays,y=n))+
geom_bar(stat="identity",fill="deepskyblue2")+
scale_x_discrete(limits=levels(topPays$pays))+
geom_text(aes(label=n),hjust=1.5)+
xlab(NULL)+
ylab(NULL)+
ggtitle(label = paste("Top",input$nbPays,"des nationalités des",
isolate(input$prenom),"dans Wikidata"))+
coord_flip()
})
})