-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
160 lines (120 loc) · 4.6 KB
/
app.R
File metadata and controls
160 lines (120 loc) · 4.6 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
159
160
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# library(gganimate)
library(ggplot2)
library(gganimate)
# Define UI for application that draws a histogram
ui <- fluidPage(
fluidRow( class = "plotRow",
# TODO height
column(class = "myplot", width = 12,
plotOutput(outputId = "particle", height = "600px"))
),
fluidRow( class = "controlRow",
style = 'height:10vh',
column(width = 3,
fileInput("file1", "Choose CSV File w/ persons",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
column(width = 3,
fileInput("file2", "Choose CSV File w/ antenna's",
accept = c(
"text/csv",
"text/comma-separated-values, text/plain",
".csv"))
),
column(width = 3,
uiOutput("sliders")
)
# column(width = 3,
# downloadButton(outputId = "savegif",
# label = "Save plot to gif"))
),
fluidRow(
column(width = 3,
uiOutput("sel_agents")
)),
tags$head(tags$style("
.plotRow{height:700px;}
.myplot{height:600px}
.controlRow{height:100px;}
")
)
)
# Define server logic
server <- function(input, output) {
# set file size limit
options(shiny.maxRequestSize = 100*1024^2)
#################### read data from csv ########################
# agents
agents <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, skip = 2, stringsAsFactors = FALSE, header = FALSE)
return(tbl)
})
# antenna's
antenna <- reactive({
inFile <- input$file2
if (is.null(inFile))
return(NULL)
tbl <- read.csv(inFile$datapath, stringsAsFactors = FALSE, header = FALSE)
return(tbl)
})
####################################################################
#################### create UI elements ############################
output$sliders <- renderUI(
if (!is.null(agents)) {
sliders <- sliderInput("time", "TIME:", min = min(agents()[,1]),
max(agents()[,1]), value = min(agents()[,1]), step = 1,
animate = animationOptions(interval = 500, loop = TRUE))
})
output$sel_agents <- renderUI(
if(!is.null(agents)){
sel_agents <- selectizeInput(inputId = "sa", label = "Select agent:", choices = c("ALL", agents()[,2]), selected = "ALL")
})
####################################################################
part_anim <- function(){
df1 <- agents()[agents()[,1] == input$time, ]
#df1 <- agents()[agents()[,1] <= input$time, ]
if(input$sa == "ALL"){
df1
} else {
df1 <- df1[df1[,2] == input$sa,]
}
df2 <- antenna()[]
p <- ggplot(data = df1, aes(x = df1[,3], y = df1[,4])) + geom_point(aes(color = df1[,2], group = df1[,2], shape = ifelse(is.na(df1[,5]), "NoSIM", "SIM"), size = ifelse(is.na(df1[,5]), "NoSIM", "SIM"))) + scale_shape_manual(name = "", values = c(NoSIM = 2, SIM = 4)) + scale_size_manual(name = "", values = c(NoSIM = 2, SIM = 4))
p <- p + geom_point(data = df1[!is.na(df1[,5]), ], aes(x = df1[!is.na(df1[,5]), 3], y = df1[!is.na(df1[,5]), 4], color = df1[!is.na(df1[,5]), 2], group = df1[!is.na(df1[,5]), 2]))
p <- p + xlim(0,10) + ylim(0,10)
p <- p + geom_point(shape = 8, size = 6, data = df2, aes(x = df2[,2], y = df2[,3]), colour = "#CC0000")
p <- p + xlab(label = "Longitude") + ylab("Latitude")
p
}
output$particle <- renderPlot({
req(part_anim)
part_anim()
})
# TODO gif save
# output$savegif <- downloadHandler(
# filename = "test.gif",
# content = function(file){
# req(part_anim())
# saveGIF(
# for(i in 1:max(input$time)){
# part_anim()
# }, movie.name = "test.gif", interval = 0.1)
# file.rename("test.gif", file)
# })
}
# Run the application
shinyApp(ui = ui, server = server)