-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04b-generating-html.r
213 lines (187 loc) · 6.74 KB
/
04b-generating-html.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
#===============================================================================
# File-Name: 02-generating-html.R
# Date: May 6, 2019
# Paper: Who Leads? Who Follows? Measuring Issue Attention and Agenda Setting
# by Legislators and the Mass Public Using Social Media Data
# Journal: American Political Science Review
# Authors: Pablo Barbera, Andreu Casas, Jonathan Nagler, Patrick J. Egan,
# Richard Bonneau, John Jost, Joshua A. Tucker
# Purpose: generate html pages for dashboard
# Data In:
# dashboard/*
#===============================================================================
# DATA
#===============================================================================
load("dashboard/qois.rdata")
# load("dashboard/rs-tweets.rdata")
# load("dashboard/media-rs-tweets.rdata")
# load("dashboard/top-mcs.rdata")
K <- 100 #topics
for (k in 1:K){
## generating topic dropdown
topic.list <- 1:100
# topic.list <- scan("dashboard/topic-list.txt", what="character", sep="\n")
dropdown <- paste0("<option value=topic-", 1:K, ".html>", topic.list, "</option>")
dropdown[k] <- gsub('option ', 'option selected="selected" ', dropdown[k])
# generating lines with values of interest
qois1 <- paste0(
"Topic usage by elites: ", sprintf('%0.2f', qois$prop[k]), "% all, ",
'<font color="blue">', sprintf('%0.2f', qois$prop_lr[k]), "% LR Deputy</font>, ",
'<font color="orange">', sprintf('%0.2f', qois$prop_majority[k]), "% Majority Deputy</font>, ",
'<font color="red">', sprintf('%0.2f', qois$prop_nupes[k]), "% Nupes Deputy</font>, ",
'<font color="brown">', sprintf('%0.2f', qois$prop_rn[k]), "% RN Deputy</font>.</br>")
######## to be done ########
qois2 <- NULL
# qois2 <- paste0('Top Members of Congress:',
# topmcs$text[topmcs$topic==k][1], ',',
# topmcs$text[topmcs$topic==k][2], ',',
# topmcs$text[topmcs$topic==k][3], ',',
# topmcs$text[topmcs$topic==k][4], ',',
# topmcs$text[topmcs$topic==k][5], '</br>')
######## to be done ########
qois3 <- NULL
# qois3 <- paste0(
# "Topic usage by media and public: ", sprintf('%0.2f', qois$media[k]), "% Media, ",
# sprintf('%0.2f', qois$public[k]), "% informed public, ",
# sprintf('%0.2f', qois$random[k]), "% random users, ",
# '<font color="blue">', sprintf('%0.2f', qois$democrats[k]), "% Democratic supporters</font>, ",
# '<font color="red">', sprintf('%0.2f', qois$republicans[k]), "% Republican supporters</font>.</br>")
# image line
img.line <- paste0("<img src='img/words-plot-", k, ".png'>")
# data location
d.loc <- paste0('"data/ts-', k, '.csv", // path to CSV file')
# html text
html <- paste0(
'<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="js/dygraph-combined.js"></script>
<script src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>
#sitemap { position: absolute; left: 50%; margin-left: -400px; top: 1%; margin-top: 50 px; }
#header1 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 30px; }
#div1 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 90px; }
#div2 { position: absolute; left: 50%; margin-left: 150; top: 0%; margin-top: 90px; }
#div3 { position: absolute; left: 50%; margin-left: -500; top: 0%; margin-top: 440px; }
#div4 { position: absolute; left: 50%; margin-left: -500; top: 0%; margin-top: 480px; }
#header2 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 550px; }
#div5 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 600px; }
#div6 { position: absolute; left: 50%; margin-left: 0; top: 0%; margin-top: 600px; }
#header3 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 1250px; }
#div7 { position: absolute; left: 50%; margin-left: -500px; top: 0%; margin-top: 1300px; }
#div8 { position: absolute; left: 50%; margin-left: 0; top: 0%; margin-top: 1300px; }
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<body>
<div id="sitemap">
<b> « <a href="index.html">Back to Index</a> • Topic selection: </b>
<select onchange="if (this.value) window.location.href=this.value" class="input-xxlarge">
',
paste(dropdown, collapse="\n"),
'
</select>
</div>
<div id="header1"><h3>Topic Usage Over Time:</h3></div>
<div id="div1" style="width:600px">
</div>
<div id="div2" style="width:300px">
',
img.line,
'
</div>
<div id="div3">
<form class="form-inline">
<b>Display: </b>
<label class="checkbox-inline">
<input type="checkbox" id=0 onClick="change(this)" checked> LR Deputy
</label>
<label class="checkbox-inline">
<input type="checkbox" id=1 onClick="change(this)" checked> Majority Deputy
</label>
<label class="checkbox-inline">
<input type="checkbox" id=2 onClick="change(this)" checked> Nupes Deputy
</label>
<label class="checkbox-inline">
<input type="checkbox" id=3 onClick="change(this)" checked> RN Deputy
</label>
• <b> Smoothing period:</b>
<input type="number" value="7" id="input" min="1" step="1" style = "width:50px;height:30px" onchange="changeRoll()">
days
</form>
</div>
<div id="div4">
',
qois1,
# qois2, qois3,
'
</div>
<div id="header2"><h3>Sample of representative tweets by Members of Congress:</h3></div>
<div id="div5" style="width:500px">
',
# rs$embed[rs$topic==k][1],
# rs$embed[rs$topic==k][3],
# rs$embed[rs$topic==k][5],
'
</div>
<div id="div6" style="width:500px">
',
# rs$embed[rs$topic==k][2],
# rs$embed[rs$topic==k][4],
# rs$embed[rs$topic==k][6],
'
</div>
<div id="header3"><h3>Sample of representative media tweets:</h3></div>
<div id="div7" style="width:500px">
',
# media_rs$embed[media_rs$topic==k][1],
# media_rs$embed[media_rs$topic==k][3],
# media_rs$embed[media_rs$topic==k][5],
'
</div>
<div id="div8" style="width:500px">
',
# media_rs$embed[media_rs$topic==k][2],
# media_rs$embed[media_rs$topic==k][4],
# media_rs$embed[media_rs$topic==k][6],
'
</div>
<br><br>
</div>
<script type="text/javascript">
var g;
g = new Dygraph(
document.getElementById("div1"),
',
d.loc,
'
{
showRangeSelector: false,
rollPeriod: 7,
title: "",
ylabel: "Pr(topic)",
legend: "always",
labelsDivStyles: { "textAlign": "right" , "background": "none"},
colors: ["blue", "orange", "red", "brown"],
visibility: [true, true, true, true],
labelsSeparateLines: true,
labelsKMB: true,
} // options
);
function change(el) {
g.setVisibility(el.id, el.checked);
}
function changeRoll(){
value = input.value;
g.adjustRoll(value);
}
</script>
</body>
</html>
')
writeLines(html, con=paste0("dashboard/files/topic-", k, ".html"))
}