server.R 11.1 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)
Stephanie's avatar
Stephanie committed
9

10

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

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

    #coconut.df$timestamp <- format(coconut.df$timestamp, format="%d-%B-%Y %H:%M:%S")
39
    
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
40
    #print(coconut.df)
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
    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}

    
93
    # setting the table style
94 95 96 97 98 99 100 101 102 103 104 105 106
    datatable(
              cbind(coconut.df), 
              options = list(
                searching = TRUE,
                autoWidth = TRUE,
                rownames = FALSE,
                scroller = TRUE,
                scrollX = TRUE,
                scrollY = "500px",
                fixedHeader = TRUE,
                class = 'cell-border stripe'
              )
    )
107
  })
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
108 109 110 111 112 113 114
  #last coconut_name
  output$lastLoadedNut<- renderText({
    if (is.null(input$nut))
      return(NULL)
    else lastCoConUT
  })
  
115 116
  #CoCoQuest Data import
  output$quest <- renderDataTable({
117 118 119 120
    inFile <- input$quest
    if (is.null(inFile))
      return(NULL)
    
121 122
    #loading CoCoQuest data
    json_data_quest <<- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
123
    
124
    lastCoCoQuest <<- inFile$name
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
125 126
    totalST <<- c(anytime(as.numeric(json_data_quest$startTime)/1000))
   
127
    #unix  parsed
128 129
    dat <- do.call(rbind, json_data_quest$tasks$data)
  
130 131 132 133 134
    endtimes <<- c(anytime(dat$endtime))
    
    #endtimes <<- format(dat$endtime, format="%d-%B-%Y %H:%M:%S")
    print("endtime quest")
    print(endtimes)
135
    tasks <- rep(1:length(json_data_quest$tasks$data), sapply(json_data_quest$tasks$data, nrow))
136

137
    together <<- c(tasks, endtimes)
138 139
    print("together")
    print(together)
140 141
    all <- rbind(together)

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

  #cocoQuest Starttime
161 162 163
  output$totalStarttime <- renderText({
    if (is.null(input$quest))
      return(NULL)
164
    else {
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
165
      format(totalST, format="%d-%B-%Y %H:%M:%S")
166 167
      
    }
168
  })
169
  
170 171 172 173 174 175 176
  #Continue to control page  
  observeEvent(input$controlSide, {
    
    updateTabItems(session, "tabs",selected = "controls")
    
  })
  
177 178 179
#CONTROLLS PAGE
  
  #Title Input
180
  output$text <- renderText({ input$title })
181
  
182
  #Histogram
183
  
184
  output$input_hist_x <- renderUI({
185 186 187
    if (is.null(input$nut))
      return(NULL)
    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) 
188 189 190
  })
  
  output$input_hist_y <- renderUI({
191 192
    if (is.null(input$nut))
      return(NULL)
193
    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) 
194 195
  })
  
196
  
197 198
  #Timeline

199
  output$input_time_x <- renderUI({
200 201
    if (is.null(input$nut))
      return(NULL)
202
    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) 
203
  })
204
  
205
  output$input_time_y <- renderUI({
206 207
    if (is.null(input$nut))
      return(NULL)
208
    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) 
209 210
  })
  
211
  
212
  #Map
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
213 214 215 216 217 218 219 220 221 222
  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) 
    })
  #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) 
  #})
223
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
224 225 226 227 228
 # 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) 
  #})
229
  
230
  
231 232
  #Linegraph
 
233
  output$input_line_x <- renderUI({
234 235
    if (is.null(input$nut))
      return(NULL)
236
    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) 
237 238 239
  })
  
  output$input_line_y <- renderUI({
240 241
    if (is.null(input$nut))
      return(NULL)
242
    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) 
243
  })
244
  
245
  
246 247 248 249 250 251
  #Continue to dashboard page  
  observeEvent(input$dashboardSite, {
    
    updateTabItems(session, "tabs",selected = "dashboard")
    
  })
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  
#DASHBOARD PAGE  
  
  #HISTOGRAM
  output$hist_box <- renderUI({
    if(input$check_hist)
      box(  title="Histogram",
           status= "warning",
          solidHeader = TRUE,
          collapsible = TRUE,
          plotOutput("plot1", height = 250))
  })
  
  
  #TIMELINE
  output$time_box <- renderUI({
    if(input$check_time)
      box(  title="Timeline",
            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
283
            leafletOutput("map", height = 250))
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
284 285
  })
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
286 287 288 289
  # actual plot
  output$map <- renderLeaflet({
    print("wird aufgerufen")
    m <<- leaflet()
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
290 291
    x=1
    m <- addTiles(m)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
292 293
    for(x in 1:length(coconut.df$gps.latitude)){
      m <- addMarkers(m,lng=as.numeric(longitude[x]), lat=as.numeric(latitude[x])) #addPolylines does not work
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
294 295
      x=x+1}
    m
296 297
  })
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
298
  
299 300 301 302 303 304 305 306 307 308
  #LINEGRAPH
  output$line_box <- renderUI({
    if(input$check_line)
      box(  title="Linegraph",
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("plot1", height = 250))
  })
  
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  # MAP
 
  
  
  
  #function connectTheDots(data){
  #   c = []
  #  for(i in data._layers) {
  #     x = data._layers[i]._latlng.lat;
  #    y = data._layers[i]._latlng.lng;
  #   c.push([x, y]);
  #}
  #return c
  #}
  
  #pathCoords = connectTheDots(window.geojson);
  #pathLine = L.polyline(pathCoords).addTo(m)
  
  
  
  
330
  #DownloadButton
331 332
  #TODO: MAKE IT WORK!
  observeEvent(input$screenshot,{
333
    #webshot::install_phantomjs()
334
    cdat <<- session$clientData
335
    url <- paste0(cdat$url_hostname,":", cdat$url_port,"/")
336
    print(url)
337 338
    #URL <- "http://rstudio.github.io/leaflet/"
    #appshot("cocoVisR/", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
339
    #webshot(url,delay = 5.0)
340
    #knit("dashboard.png")
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
341 342 343
    #port <- cdat$url_port
    #mapshot(m, file="~/Rplot.png")
    
344
    #webshot(url, "dashboard.png", delay = 20.0) # does NOT WORK 
Stephanie Wegscheidl's avatar
Stephanie Wegscheidl committed
345 346 347 348 349 350 351 352 353 354 355 356
    #appdir <- system.file("examples", "01_hello", package="shiny")
    #print("appdir")
    #print(appdir)
    #appshot(appdir, "01_hello.png")
    
    leaflet.print(m)
    
    
    
    
    
    
357 358
    
  })
359
  # url <- paste0(cdat$url_protocol,"//",cdat$url_hostname,":", cdat$url_port, cdat$url_pathname,cdat$url_search)
360 361 362 363 364 365 366 367 368
  #output$downButton <- downloadHandler(
   # filename="dashboard.png",
    #content=function(file){
     # observeEvent(input$downButton, {
      #appshot("cocoVisR/dashboard", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
 
      #}) 
    #}
  #)
369
 
370 371 372 373 374 375 376 377
  
  
  
  
  
#SOME STUFF  
  
  
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  #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
395
}