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

8

9
server <- function(input, output, session) {
Stephanie's avatar
Stephanie committed
10
  set.seed(122)
11 12 13 14 15 16 17 18 19

#DATA PAGE
  
  #last coconut_name
  output$lastLoadedNut<- renderText({
    if (is.null(input$nut))
      return(NULL)
    else lastCoConUT
  })
20
  
21
  #CoConUT Data import 
22 23
  output$nut <- renderDataTable({
    inFile <- input$nut
24 25
    if (is.null(inFile))
      return(NULL)
26
    
27
    #loading CoConUT data
28
    json_data_coco <- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
29
    coconut.df <<- json_data_coco[["data"]]
30 31
    
    lastCoConUT <<- inFile$name
32
    print("coconut:")
33 34

    #coconut.df$timestamp <- format(coconut.df$timestamp, format="%d-%B-%Y %H:%M:%S")
35
    
36
    print(coconut.df$timestamp)
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
    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}

    
89
    # setting the table style
90 91 92 93 94 95 96 97 98 99 100 101 102
    datatable(
              cbind(coconut.df), 
              options = list(
                searching = TRUE,
                autoWidth = TRUE,
                rownames = FALSE,
                scroller = TRUE,
                scrollX = TRUE,
                scrollY = "500px",
                fixedHeader = TRUE,
                class = 'cell-border stripe'
              )
    )
103
  })
104 105 106

  #CoCoQuest Data import
  output$quest <- renderDataTable({
107 108 109 110
    inFile <- input$quest
    if (is.null(inFile))
      return(NULL)
    
111 112
    #loading CoCoQuest data
    json_data_quest <<- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
113
    
114
    lastCoCoQuest <<- inFile$name
115
    
116 117 118 119 120
    #json_data_quest$unix_starttime <- as.numeric(json_data_quest$startTime)/1000
    #json_data_quest$unix_endtime <- as.numeric(json_data_quest$endTime)/1000
    #totalStarttime <<- anytime(c(json_data_quest$unix_starttime))
    #totalEndtime <<- anytime(json_data_quest$unix_endtime)
    
121
    
122
    #json_data_quest$unix_starttime <- format(json_data_quest$unix_starttime, format="%d-%B-%Y %H:%M:%S")
123
    
124
    #unix  parsed
125 126
    dat <- do.call(rbind, json_data_quest$tasks$data)
  
127 128 129 130 131
    endtimes <<- c(anytime(dat$endtime))
    
    #endtimes <<- format(dat$endtime, format="%d-%B-%Y %H:%M:%S")
    print("endtime quest")
    print(endtimes)
132
    tasks <- rep(1:length(json_data_quest$tasks$data), sapply(json_data_quest$tasks$data, nrow))
133

134
    together <<- c(tasks, endtimes)
135 136
    print("together")
    print(together)
137 138
    all <- rbind(together)

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

  #cocoQuest Starttime
158 159 160
  output$totalStarttime <- renderText({
    if (is.null(input$quest))
      return(NULL)
161 162 163 164
    else {
      format(totalStarttime, format="%d-%B-%Y %H:%M:%S")
      
    }
165
  })
166
  
167 168 169
#CONTROLLS PAGE
  
  #Title Input
170
  output$text <- renderText({ input$title })
171
  
172
  #Histogram
173
  
174
  output$input_hist_x <- renderUI({
175 176 177
    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) 
178 179 180
  })
  
  output$input_hist_y <- renderUI({
181 182
    if (is.null(input$nut))
      return(NULL)
183
    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) 
184 185
  })
  
186
  
187 188
  #Timeline

189
  output$input_time_x <- renderUI({
190 191
    if (is.null(input$nut))
      return(NULL)
192
    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) 
193
  })
194
  
195
  output$input_time_y <- renderUI({
196 197
    if (is.null(input$nut))
      return(NULL)
198
    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) 
199 200
  })
  
201
  
202 203
  #Map

204
  output$input_map_x <- renderUI({
205 206
    if (is.null(input$nut))
      return(NULL)
207
    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) 
208 209 210
  })
  
  output$input_map_y <- renderUI({
211 212
    if (is.null(input$nut))
      return(NULL)
213
    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) 
214 215
  })
  
216
  
217 218
  #Linegraph
 
219
  output$input_line_x <- renderUI({
220 221
    if (is.null(input$nut))
      return(NULL)
222
    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) 
223 224 225
  })
  
  output$input_line_y <- renderUI({
226 227
    if (is.null(input$nut))
      return(NULL)
228
    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) 
229
  })
230
  
231
  
232
  
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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
  
#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,
            plotOutput("plot1", height = 250))
  })
  
  #LINEGRAPH
  output$line_box <- renderUI({
    if(input$check_line)
      box(  title="Linegraph",
            status= "warning",
            solidHeader = TRUE,
            collapsible = TRUE,
            plotOutput("plot1", height = 250))
  })
  
  #DownloadButton
278 279
  #TODO: MAKE IT WORK!
  observeEvent(input$screenshot,{
280
    #webshot::install_phantomjs()
281
    cdat <<- session$clientData
282
    url <- paste0(cdat$url_hostname,":", cdat$url_port,"/")
283
    print(url)
284 285 286 287 288 289
    #URL <- "http://rstudio.github.io/leaflet/"
    #appshot("cocoVisR/", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
    #webshot(URL, "dashboard.png",delay = 5.0)
    #knit("dashboard.png")
    port <- cdat$url_port
    #webshot(url, "dashboard.png", delay = 20.0) # does NOT WORK 
290 291
    
  })
292
  # url <- paste0(cdat$url_protocol,"//",cdat$url_hostname,":", cdat$url_port, cdat$url_pathname,cdat$url_search)
293 294 295 296 297 298 299 300 301
  #output$downButton <- downloadHandler(
   # filename="dashboard.png",
    #content=function(file){
     # observeEvent(input$downButton, {
      #appshot("cocoVisR/dashboard", file = "dashboard.png", port = getOption("shiny.port"), envvars = NULL)
 
      #}) 
    #}
  #)
302
 
303 304 305 306 307 308 309 310
  
  
  
  
  
#SOME STUFF  
  
  
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  #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
328
}