/
server.R
50 lines (37 loc) · 1.21 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
library(ggplot2)
shinyServer(function(input, output, session) {
data <- reactive({
if (is.null(input$file))
return(NULL)
else if (identical(input$format, 'CSV'))
return(read.csv(input$file$datapath))
else
return(read.delim(input$file$datapath))
})
observe({
df <- data()
str(names(df))
if (!is.null(df)) {
updateSelectInput(session, 'x', choices = names(df))
updateSelectInput(session, 'y', choices = names(df))
updateSelectInput(session, 'color', choices = c('None', names(df)))
updateSelectInput(session, 'facet_row', choices = c(None='.', names(df)))
updateSelectInput(session, 'facet_col', choices = c(None='.', names(df)))
}
})
output$plot <- renderPlot({
if (is.null(data()))
return(NULL)
p <- ggplot(data(), aes_string(x=input$x, y=input$y)) + geom_point()
if (input$smooth)
p <- p + geom_smooth()
if (input$color != 'None')
p <- p + aes_string(color=input$color)
facets <- paste(input$facet_row, '~', input$facet_col)
if (facets != '. ~ .')
p <- p + facet_grid(facets)
if (input$jitter)
p <- p + geom_jitter()
print(p)
}, height=700)
})