-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
140 lines (126 loc) · 5.38 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
# Set required pacakges - should be REQUIRE, not LIBRARY
require(shiny)
require(datasets)
require(foreign)
require(Hmisc)
require(memisc)
require(ggplot2)
# Set options for number of digits in numeric output
options(digits=3)
#############################################
## READ DATA - Australian Election Study 2010
#############################################
# This data import option uses Hmisc spss.get, which allows storage of variable labels
# Note: the "use.value.labels=TRUE" option uses the value labels
# to convert these variables to FACTORS in R
# For a brief descripton of factors, see:
# - http://www.statmethods.net/input/datatypes.html
# - http://statistics.ats.ucla.edu/stat/r/modules/factor_variables.htm
mpgdata <- spss.get("./aes_subset/aes2010_subset.sav",
use.value.labels=TRUE, to.data.frame=TRUE)
############################################
############################################
# The remainder of this code is developed by extending on the Shiny tutorial example
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive(function() {
paste(input$yvariable, " ~ ", input$xvariable)
})
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
formulaText()
})
# Generate a plot (based on "chart_type")
# of the requested y variable against requested x variable and only
# include outliers if requested
# Graph examples taken from Quick-R: http://www.statmethods.net/graphs/index.html
output$chart_type <- reactivePlot(function() {
# Boxplot
if(input$chart_type == "boxplot") {
boxplot(as.formula(formulaText()),
data = mpgdata,
outline = input$outliers,
xlab=label(mpgdata[input$xvariable]),
ylab=label(mpgdata[input$yvariable]))
}
# Scatterplot
# Need to figure out how to remove NAs introduced by coercion
if(input$chart_type == "scatter") {
xy <- cbind(mpgdata[input$xvariable],mpgdata[input$yvariable])
# plot(as.numeric(input$xvariable), as.numeric(input$yvariable), main=output$caption,
# xlab=label(mpgdata[input$xvariable]),
# ylab=label(mpgdata[input$yvariable]),
# xlim=c(min(na.omit(mpgdata[input$xvariable])),
# max(na.omit(mpgdata[input$xvariable]))),
# ylim=c(min(na.omit(mpgdata[input$yvariable])),
# max(na.omit(mpgdata[input$yvariable]))),
# pch=19
plot(as.numeric(xy[,1]),as.numeric(xy[,2]), main=output$caption,
xlab=label(mpgdata[input$xvariable]),
ylab=label(mpgdata[input$yvariable]),
pch=19
)
}
# Barplot - simple
if(input$chart_type == "barplot-simple") {
# counts <- table(input$xvariable)
# barplot(counts, main=formulaText(),
# xlab=label(mpgdata[input$xvariable]))
summary(mpgdata$a1)
barplot(table(mpgdata[input$xvariable]),
main=summary(mpgdata$a1),
xlab=label(mpgdata[input$xvariable]),
ylab="Count")
}
# Barplot - grouped
# Currently producing a solid blue box??
if(input$chart_type == "barplot-grouped") {
xy <- cbind(mpgdata[input$xvariable],mpgdata[input$yvariable])
counts <- table(xy[,1], xy[,2])
barplot(counts, main=formulaText(),
xlab=label(mpgdata[input$xvariable]),
col=c("darkblue","red"),
legend = rownames(counts), beside=TRUE)
# boxplot(as.formula(formulaText()),
# data = mpgdata,
# outline = input$outliers,
# xlab=label(mpgdata[input$xvariable]),
# ylab=label(mpgdata[input$yvariable]))
}
# Line plot
if(input$chart_type == "line") {
xy <- cbind(mpgdata[input$xvariable],mpgdata[input$yvariable])
par(pch=22, col="blue") # plotting symbol and color
opts = c("p","l","o","b","c","s","S","h")
# plot(range(input$xvariable), range(input$yvariable), main=output$caption)
# lines(input$xvariable, input$yvariable, type="l")
plot(range(as.numeric(xy[,1])), range(as.numeric(xy[,2])), main=output$caption,
xlab=label(mpgdata[input$xvariable]),
ylab=label(mpgdata[input$yvariable]))
lines(as.numeric(xy[,1]), as.numeric(xy[,2]), type="l")
}
# Scatterplot - ggplot
if(input$chart_type == "scatter-gg") {
# Push selected variables to the xy dataframe
# ggplot doesnt seem to want to use mpgdata directly
xy <- cbind(mpgdata[input$xvariable],mpgdata[input$yvariable])
p <- qplot(xy[,1],
xy[,2],
data=xy,
na.rm=TRUE
)
# Add labels
p <- p + labs(title = paste(formulaText(),
"Spearman Correlation: ",
cor(as.numeric(xy[,1]), as.numeric(xy[,2]),
method = "spearman", use="complete")))
p <- p + labs(x = label(mpgdata[input$xvariable]))
p <- p + labs(y = label(mpgdata[input$yvariable]))
# Jitter points around origin
p <- p + geom_point(position=position_jitter(width=.2,height=.2))
print(p)
}
})
})