-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
158 lines (126 loc) · 4.01 KB
/
server.R
File metadata and controls
158 lines (126 loc) · 4.01 KB
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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(ggplot2)
library(plyr)
library(reshape)
library(scales)
library(DT)
library(stringr)
theme_set(theme_bw())
shinyServer(function(input, output) {
#reactive dataset
reac_d = reactive({
g_loadings = c(.50, .60, .75,
.65, .70, .55,
.80, .45, .30)
group1_loadings = c(.50, .50, .50,
0, 0, 0,
0, 0, 0)
group2_loadings = c(0, 0, 0,
.50, .50, .50,
0, 0, 0)
group3_loadings = c(0, 0, 0,
0, 0, 0,
.50, .50, .50)
d = data.frame(g_loadings,
group1_loadings,
group2_loadings,
group3_loadings)
d$specificity = apply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}
)
d$time_1 = rep(100, nrow(d))
d$time_2 = d$time_1 +
input$g_change * g_loadings +
input$group1_change * d$group1_loadings +
input$group2_change * d$group2_loadings +
input$group3_change * d$group3_loadings +
input$specificity_change * d$specificity
d$change = d$time_2 - d$time_1
return(d)
})
#long version, reactive
reac_d_long = reactive({
#fetch data
d = reac_d()
d_long = data.frame(time_1 = d$time_1,
time_2 = d$time_2,
indicator = str_c("I", 1:9))
d_long = melt(d_long, id.vars = "indicator")
return(d_long)
})
#before and after means
output$plot <- renderPlot({
#fetch data
d_long = reac_d_long()
#plot
ggplot(d_long, aes(indicator, value, fill = variable, group = variable)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(breaks = seq(0, 200, 10),
limits=c(80, NA),
oob = rescale_none) +
scale_fill_discrete(labels = c("Time 1", "Time 2")) +
xlab("Indicator variables") + ylab("Score")
})
# Jensen's method scatter plot
output$plot2 = renderPlot({
#fetch data
d = reac_d()
d$time_1 = NULL
d$time_2 = NULL
change = d$change
d$change = NULL
d_long2 = melt(d)
d_long2$change = rep(change, length = nrow(d_long2))
#plot
ggplot(d_long2, aes(value, change, color = variable)) +
geom_point() +
geom_smooth(method = "lm", se = F, fullrange = T) +
scale_color_discrete(labels = c("general factor",
"group factor 1",
"group factor 2",
"group factor 3",
"specificity")
) +
xlab("Factor loading") + ylab("Change in scores") +
scale_x_continuous(breaks = seq(0, 1, .05)) +
scale_y_continuous(breaks = seq(-30, 30, 1))
})
output$table = DT::renderDataTable({
#fetch data
d = reac_d()
d$time_1 = NULL
d$time_2 = NULL
colnames(d) = c("general factor", "group factor 1", "group factor 2", "group factor 3",
"specificity", "change")
cor = round(cor(d), 2)
cor = as.data.frame(cor)
return(cor)
},
options = list(searching = F,
ordering = F,
paging = F,
info = F))
output$table2 = DT::renderDataTable({
#fetch data
d = reac_d()
d$change = d$time_2 - d$time_1
d$time_1 = NULL
d$time_2 = NULL
# colnames(d) = c("general factor", "group factor 1", "group factor 2", "group factor 3",
# "specificity", "change")
return(d)
},
options = list(searching = F,
ordering = F,
paging = F,
info = F))
})