-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.R
More file actions
78 lines (71 loc) · 3.26 KB
/
server.R
File metadata and controls
78 lines (71 loc) · 3.26 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
library(shiny)
library(ggplot2)
library(dplyr)
library(broom)
grouping <- c('cyl',"vs","am","gear")
varmap <- list ('Displacement' ='disp',
'Gross horsepower'='hp',
'Rear axle ratio'='drat',
'Weight'='wt',
'1/4 mile time'='qsec',
'Number of carburetors'='carb')
groupmap <- list(
'None'='none',
#'Number of cylinders'='cyl',
'V/S'='vs',
'Transmission'='am')
##'Number of forward gears'='gear')
mainplot <- function(variable,group){
plotmap <- ggplot(data=mtcars,aes_string(x=variable,y="mpg"))
if (group == "none"){
baseplot <- plotmap + geom_point(size=3,color="salmon")
} else {
baseplot <-plotmap + geom_point(aes_string(color=group),size=3) + facet_grid(paste0(".~",group))
}
baseplot + geom_smooth(method="lm")
}
my_group_by <- function(datatb,group){
#Takes a data table and a character string
if(group=='none'){
return(datatb)
}else{
group_by_(datatb,group)
}
}
#make these two variable categorical
mtcars <- mtcars %>% mutate(am=as.factor(am))%>%mutate(vs=as.factor(vs))
shinyServer(
function(input,output){
output$edaplot <- renderPlot({
mainplot(varmap[[input$variable]],groupmap[[input$groupby]])
})
output$modelplot <- renderPlot(
{
baseplot <- mtcars %>%
my_group_by(groupmap[[input$groupby]])%>%
do(tidy(lm(as.formula(paste0("mpg~",varmap[[input$variable]])),data=.),conf.int=T))%>%
ggplot(aes_string('term','estimate'))+
geom_errorbar(aes(ymin=conf.low,ymax=conf.high))
if (groupmap[[input$groupby]]=="none"){
baseplot + geom_point(color="salmon",size=3)
}else{
baseplot + geom_point(aes_string(color=groupmap[[input$groupby]]),size=3)
}
})
output$residplot <- renderPlot(
{
baseplot <- mtcars %>%
my_group_by(groupmap[[input$groupby]])%>%
do(augment(lm(as.formula(paste0("mpg~",varmap[[input$variable]])),data=.)))%>%
ggplot(aes_string(varmap[[input$variable]],'.resid')) +
geom_hline(yintercept=0)
if (input$groupby=='None'){
baseplot + geom_point(color='salmon',size=3)
}else{
baseplot + facet_grid(paste0('.~',groupmap[[input$groupby]])) +
geom_point(aes_string(color=groupmap[[input$groupby]]),size=3)
}
}
)
}
)