server.R 12.6 KB
Newer Older
Stephanie's avatar
Stephanie committed
1 2

library(shiny)
3
library(jsonlite)
4
library(DT)
5
library(webshot)
6
library(knitr)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
7
library(leaflet)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
8
library(mapview)
9
library(ggplot2)
10
library(timevis)
Stephanie's avatar
Stephanie committed
11

12

13
server <- function(input, output, session) {
Stephanie's avatar
Stephanie committed
14
  set.seed(122)
15

16 17 18 19 20 21
#Continue to data page  
  observeEvent(input$dataSite, {
    
    updateTabItems(session, "tabs",selected = "data")
   
  })
22 23
#DATA PAGE
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
24
  
25
  
26
  #CoConUT Data import 
27 28
  output$nut <- renderDataTable({
    inFile <- input$nut
29 30
    if (is.null(inFile))
      return(NULL)
31
    
32
    #loading CoConUT data
33
    json_data_coco <- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
34
    coconut.df <<- json_data_coco[["data"]]
35
    
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
36
    
37
    lastCoConUT <<- inFile$name
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
38
    #print("coconut:")
39 40

    #coconut.df$timestamp <- format(coconut.df$timestamp, format="%d-%B-%Y %H:%M:%S")
41 42
    print("HERE!!!!!!")
    print(as.numeric(coconut.df[["timestamp"]]))
43
    
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
44
    #print(coconut.df)
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
    if(is.null(coconut.df$timestamp)){}
    else{unixTime<<-coconut.df$timestamp}
    
    if(is.null(coconut.df$gps.latitude)){}
    else{latitude<<-coconut.df$gps.latitude}
    
    if(is.null(coconut.df$gps.longitude)){}
    else{longitude<<-coconut.df$gps.longitude}
    
    if(is.null(coconut.df$gps.speed)){}
    else{speed<<-coconut.df$gps.speed}
    
    if(is.null(coconut.df$gps.accuracy)){}
    else{gpsAcc<<-coconut.df$gps.accuracy}
    
    if(is.null(coconut.df$light.lux)){}
    else{lux<<-coconut.df$light.lux}
    
    if(is.null(coconut.df$acc.coordinates)){}
    else{accCoord<<-coconut.df$acc.coordinates}
    
    if(is.null(coconut.df$acc.accuracy)){}
    else{accAcc<<-coconut.df$acc.accuracy}
    
    if(is.null(coconut.df$bluetooth.numOfBTDevices)){}
    else{bluetooth<<-coconut.df$bluetooth.numOfBTDevices}
    
    if(is.null(coconut.df$ble.bpm)){}
    else{bleBpm<<-coconut.df$ble.bpm}
    
    if(is.null(coconut.df$ble.bpmlist)){}
    else{bleBpmList<<-coconut.df$ble.bpmlist}
    
    if(is.null(coconut.df$ble.hrv)){}
    else{bleHrv<<-coconut.df$ble.hrv}
    
    if(is.null(coconut.df$ble.hrvlist)){}
    else{bleHrvList<<-coconut.df$ble.hrvlist}
    
    if(is.null(coconut.df$activity.name)){}
    else{activityName<<-coconut.df$activity.name}
    
    if(is.null(coconut.df$activity.confidence)){}
    else{activityConf<<-coconut.df$activity.confidence}
    
    if(is.null(coconut.df$screen.status)){}
    else{screenstatus<<-coconut.df$screen.status}
    
    if(is.null(coconut.df$interaction.touch)){}
    else{touch<<-coconut.df$interaction.touch}

    
97
    # setting the table style
98 99 100 101 102 103 104 105 106 107 108 109 110
    datatable(
              cbind(coconut.df), 
              options = list(
                searching = TRUE,
                autoWidth = TRUE,
                rownames = FALSE,
                scroller = TRUE,
                scrollX = TRUE,
                scrollY = "500px",
                fixedHeader = TRUE,
                class = 'cell-border stripe'
              )
    )
111
  })
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
112 113 114 115 116 117 118
  #last coconut_name
  output$lastLoadedNut<- renderText({
    if (is.null(input$nut))
      return(NULL)
    else lastCoConUT
  })
  
119 120
  #CoCoQuest Data import
  output$quest <- renderDataTable({
121 122 123 124
    inFile <- input$quest
    if (is.null(inFile))
      return(NULL)
    
125 126
    #loading CoCoQuest data
    json_data_quest <<- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
127
    
128
    lastCoCoQuest <<- inFile$name
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
129
    totalST <<- c(anytime(as.numeric(json_data_quest$startTime)/1000))
130 131 132
    print(json_data_quest)
    

133
    #unix  parsed
134 135
    dat <- do.call(rbind, json_data_quest$tasks$data)
  
136 137 138 139 140
    endtimes <<- c(anytime(dat$endtime))
    
    #endtimes <<- format(dat$endtime, format="%d-%B-%Y %H:%M:%S")
    print("endtime quest")
    print(endtimes)
141
    tasks <- rep(1:length(json_data_quest$tasks$data), sapply(json_data_quest$tasks$data, nrow))
142 143
    #questions 
    #print("Questions:", questions)
144
    together <<- c(tasks, endtimes)
145 146
    print("together")
    print(together)
147 148
    all <- rbind(together)

149
    #setting the table style
150
    datatable(
151
      cbind(tasks, endtimes),
152
      options = list(
153 154
        searching = FALSE,
        rownames = TRUE,
155
        class = 'cell-border stripe'
156
     )
157 158
    )
  })
159 160 161 162 163 164 165
  
  #last cocoquest_name
  output$lastLoadedQuest <- renderText({
    if (is.null(input$quest))
      return(NULL)
    else lastCoCoQuest
  })
166 167

  #cocoQuest Starttime
168 169 170
  output$totalStarttime <- renderText({
    if (is.null(input$quest))
      return(NULL)
171
    else {
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
172
      format(totalST, format="%d-%B-%Y %H:%M:%S")
173 174
      
    }
175
  })
176
  
177 178
  #Continue to view page  
  observeEvent(input$viewSite, {
179
    
180
    updateTabItems(session, "tabs",selected = "views")
181 182 183
    
  })
  
184
#View PAGE
185 186
  
  #Title Input
187
  output$title <- renderText({ input$title })
188
  
189
  #barchart
190
  
191
  output$input_hist_x <- renderUI({
192
    if (is.null(input$nut))
193
     return(NULL)
194
    selectInput("in1", "x-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
195 196 197
  })
  
  output$input_hist_y <- renderUI({
198 199
    if (is.null(input$nut))
      return(NULL)
200
    selectInput("in2", "y-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
201 202
  })
  
203
  
204 205
  #Timeline

206
  output$input_time_x <- renderUI({
207 208
   if (is.null(input$nut))
     return(NULL)
209
    selectInput("in3", "x-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
210
  })
211
  
212
 output$input_time_y <- renderUI({
213 214
    if (is.null(input$nut))
      return(NULL)
215
    selectInput("in4", "y-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
216 217
  })
  
218
  
219
  #Map
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
220 221 222 223 224
  output$input_map <- renderUI({
      if (is.null(input$nut))
        return(NULL)
     selectInput("in5", "Data:", cbind("unixTime", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
    })
225
  
226 227 228 229 230 231 232 233 234 235 236
  output$input_map_x <- renderUI({
    if (is.null(input$nut))
     return(NULL)
    selectInput("in5", "x-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
  })
  
  output$input_map_y <- renderUI({
    if (is.null(input$nut))
      return(NULL)
    selectInput("in6", "y-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
  })
237
  
238
  
239
  #linechart
240
 
241
  output$input_line_x <- renderUI({
242 243
    if (is.null(input$nut))
      return(NULL)
244
    selectInput("in7", "x-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
245 246 247
  })
  
  output$input_line_y <- renderUI({
248
   if (is.null(input$nut))
249
      return(NULL)
250
    selectInput("in8", "y-axis:", cbind("unixTime", "longitude", "latitude", "speed", "gps.acc","lux", "light.acc","acc.coordinates", "acc.acc","bluetooth","ble.bpm","ble.bpmlist","ble.hrv","ble.hrvlist","activity.name", "acctivity.confidence","screen.status" ,"touch"),multiple=TRUE, selectize=TRUE) 
251
  })
252
  
253
  
254 255 256 257 258 259
  #Continue to dashboard page  
  observeEvent(input$dashboardSite, {
    
    updateTabItems(session, "tabs",selected = "dashboard")
    
  })
260
  
261
#DASHBOARD  
262
  
263
  #barchart
264 265
  output$hist_box <- renderUI({
    if(input$check_hist)
266
      box(  title="Barchart",
267 268 269
           status= "warning",
          solidHeader = TRUE,
          collapsible = TRUE,
270 271 272
          plotOutput("hist", height = 250))
  })
  
273
  #BAR VIEW
274 275
  output$hist <- renderPlot({
    
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    print("x")
    print(input$bar_x)
    
    data<- structure(list(V1=as.numeric(coconut.df[[input$bar_y]]), 
                          V2=as.numeric(coconut.df[[input$bar_x]])),
                     .Names=c("V1","V2"), row.names=c(NA,6L), class = "data.frame")
    print("y")
    print(data$V1)
    
    
    #Neues Plot
    barplot(data$V1,data$V2, 
            ylab= input$bar_y,
            xlab=input$bar_x)
    
    
    ## ALTES PLOT
   #a=1
    
    #for(a in 1:length(coconut.df$bluetooth.numOfBTDevices)){
296 297 298 299 300
     
      #g <-geom_bar(mapping = NULL, data = , stat = "count",
      #             position = "stack", width = NULL, binwidth = NULL, na.rm = FALSE,
      #             show.legend = NA, inherit.aes = TRUE)
    
301
      #g <- ggplot(coconut.df, aes("timestamp","bluetooth"))
302
      #g + geom_bar()
303
     # a=a+1}
304
    
305
      #g
306 307 308 309 310
    
    #hist(bluetooth)
    
    
    
311
  
312 313 314 315 316 317 318
  })
  
  
  #TIMELINE
  output$time_box <- renderUI({
    if(input$check_time)
      box(  title="Timeline",
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("time", height = 250))
  })
  
  #Timelineview
  output$time <- renderPlot({
    print("TIME wird aufgerufen")
    timevisOutput("timeline")
   
  })
  
  #linechart
  output$line_box <- renderUI({
    if(input$check_line)
      box(  title="Linechart",
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("plot1", height = 250))
  })
  
  #TODO: Scatterplot
  output$scat_box <- renderUI({
    if(input$check_scat)
      box(  title="Scatterplot",
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("plot1", height = 250))
  })
  
  #TODO: Boxplot
  output$box_box <- renderUI({
    if(input$check_box)
      box(  title="Boxplot",
356 357 358 359 360 361 362 363 364 365 366 367 368
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("plot1", height = 250))
  })
  
  #MAP
  output$map_box <- renderUI({
    if(input$check_map)
      box(  title="Map",
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
369
            leafletOutput("map", height = 250))
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
370 371
  })
  
372
  # MAP VIEW 
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
373
  output$map <- renderLeaflet({
374
    print("map wird aufgerufen")
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
375
    m <<- leaflet()
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
376 377
    x=1
    m <- addTiles(m)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
378
    for(x in 1:length(coconut.df$gps.latitude)){
379
      m <- addCircleMarkers(m,lng=as.numeric(longitude[x]), lat=as.numeric(latitude[x]), radius = 1, color ="red") #addPolylines does not work
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
380 381
      x=x+1}
    m
382 383
  })
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
384
  
385
  
386
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
387 388
 
  
389
  #DownloadButton
390 391
  #TODO: MAKE IT WORK!
  observeEvent(input$screenshot,{
392 393
    #if(input$check_line|input$check_time|input$check_map|input$check_hist)
    #disable("screenshot")
394
    #webshot::install_phantomjs()
395
    
396
    cdat <<- session$clientData
397
    url <- paste0(cdat$url_hostname,":", cdat$url_port,"/")
398
    print(url)
399
    
400 401
    #URL <- "http://rstudio.github.io/leaflet/"
    #appshot("cocoVisR/", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
402
    #webshot(url,delay = 5.0)
403
    #knit("dashboard.png")
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
404 405 406
    #port <- cdat$url_port
    #mapshot(m, file="~/Rplot.png")
    
407
    #webshot(url, "dashboard.png", delay = 20.0) # does NOT WORK 
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
408 409 410 411 412
    #appdir <- system.file("examples", "01_hello", package="shiny")
    #print("appdir")
    #print(appdir)
    #appshot(appdir, "01_hello.png")
    
413
    #leaflet.print(m)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
414 415 416 417 418 419
    
    
    
    
    
    
420 421
    
  })
422
  # url <- paste0(cdat$url_protocol,"//",cdat$url_hostname,":", cdat$url_port, cdat$url_pathname,cdat$url_search)
423 424 425 426 427 428 429 430 431
  #output$downButton <- downloadHandler(
   # filename="dashboard.png",
    #content=function(file){
     # observeEvent(input$downButton, {
      #appshot("cocoVisR/dashboard", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
 
      #}) 
    #}
  #)
432
 
433 434 435 436 437 438 439 440
  
  
  
  
  
#SOME STUFF  
  
  
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  #histdata <- rnorm(500)
  
  #output$plot1 <- renderPlot({
  #data <- histdata[seq_len(input$slider)]
  #hist(data)
  #})
  
  ##MAP 
  
  #m <- leaflet()
  #m <- addTiles(m)
  #x=1
  #for(x in 1:2019){
  #m <- addMarkers(m,lng=longitude[x], lat=latitude[x]) #addPolylines does not work
  #x=x+1}
  #m
  #}
Stephanie's avatar
Stephanie committed
458
}