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

library(shiny)
3
library(jsonlite)
4
library(DT)
Stephanie's avatar
Stephanie committed
5

Stephanie's avatar
Stephanie committed
6 7
server <- function(input, output) {
  set.seed(122)
8
  
9
  #CoConUT Data import and showing into data site 
10 11
  output$nut <- renderDataTable({
    inFile <- input$nut
12 13
    if (is.null(inFile))
      return(NULL)
14
    
15
    #loading CoConUT data
16
    json_data_coco <- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
17
    coconut.df <<- json_data_coco[["data"]]
18 19
    
    lastCoConUT <<- inFile$name
20 21
    print("coconut:")
    print(coconut.df)
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
    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}

    
76
    # setting the table style
77 78 79 80 81 82 83 84 85 86 87 88 89
    datatable(
              cbind(coconut.df), 
              options = list(
                searching = TRUE,
                autoWidth = TRUE,
                rownames = FALSE,
                scroller = TRUE,
                scrollX = TRUE,
                scrollY = "500px",
                fixedHeader = TRUE,
                class = 'cell-border stripe'
              )
    )
90 91
    
    
92
  })
93 94
  
  output$lastLoadedNut<- renderText({
95 96 97
    if (is.null(input$nut))
      return(NULL)
    else lastCoConUT
98 99
    
  })
100

101
  
102 103

  output$questTimes <- renderDataTable({
104 105 106 107
    inFile <- input$quest
    if (is.null(inFile))
      return(NULL)
    
108 109
    #loading CoCoQuest data
    json_data_quest <<- fromJSON(paste(inFile$datapath, sep = ""), flatten = TRUE)
110
    
111
    lastCoCoQuest <<- inFile$name
112
    
113 114
    json_data_quest$unix_starttime <- as.numeric(json_data_quest$startTime)/1000
    json_data_quest$unix_endtime <- as.numeric(json_data_quest$endTime)/1000
115 116 117
    totalStarttime <<- anytime(json_data_quest$unix_starttime)
    totalEndtime <<- anytime(json_data_quest$unix_endtime)
    
118
    
119
    #unix  parsed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    dat <- do.call(rbind, json_data_quest$tasks$data)
  
    endtimes <<- c(anytime(dat$endtime/1000))
    tasks <- rep(1:length(json_data_quest$tasks$data), sapply(json_data_quest$tasks$data, nrow))
    #count<<-0
   # y <<-1
   # x<<-1
    #numOfTasks <<- if(tasks[x]!=y)
    #                {
    #                  count<<-count+1
    #                  y<<-y+1
    #                  x<<-x+1
    #}
    #else{x<<-x+1}
   
    together <<- c(tasks, endtimes)
    
    #all <<- matrix(c(tasks,endtimes),byrow=FALSE,nrow=length(tasks))
    all <- rbind(together)
139
    
140
  
141
    #setting the table style
142
    datatable(
143
      cbind(tasks, endtimes),
144
      options = list(
145 146
        searching = FALSE,
        rownames = TRUE,
147
        class = 'cell-border stripe'
148
     )
149 150 151
    )
  })
  
152 153 154 155 156
  #CoCOQuest Data import and showing into data site 
  output$lastLoadedQuest <- renderText({
    if (is.null(input$quest))
      return(NULL)
    else lastCoCoQuest
157
    
158 159 160 161 162 163 164 165
  })
  
  output$totalStarttime <- renderText({
    if (is.null(input$quest))
      return(NULL)
    else as.POSIXct(totalStarttime)
    print(totalStarttime)
  })
166 167
  
  
Stephanie's avatar
Stephanie committed
168
  #TODO: Getting HEADER out of json to select it
169 170
  
  #Not sure if so smart but it should work that way
171
  output$input_hist_x <- renderUI({
172 173 174
    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) 
175 176 177
  })
  
  output$input_hist_y <- renderUI({
178 179 180
    if (is.null(input$nut))
      return(NULL)
    selectInput("in2", "y-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
181 182 183
  })
  
  output$input_time_x <- renderUI({
184 185 186
    if (is.null(input$nut))
      return(NULL)
    selectInput("in3", "x-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
187
  })
188
  
189
  output$input_time_y <- renderUI({
190 191 192
    if (is.null(input$nut))
      return(NULL)
    selectInput("in4", "y-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
193 194
  })
  
195
  output$input_map_x <- renderUI({
196 197 198
    if (is.null(input$nut))
      return(NULL)
    selectInput("in5", "x-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
199 200 201
  })
  
  output$input_map_y <- renderUI({
202 203 204
    if (is.null(input$nut))
      return(NULL)
    selectInput("in6", "y-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
205 206 207
  })
  
  output$input_line_x <- renderUI({
208 209 210
    if (is.null(input$nut))
      return(NULL)
    selectInput("in7", "x-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
211 212 213
  })
  
  output$input_line_y <- renderUI({
214 215 216
    if (is.null(input$nut))
      return(NULL)
    selectInput("in8", "y-axis:", as.list(coconut.df),multiple=TRUE, selectize=TRUE) 
217
  })
218
  
219 220
  
  
221
  
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  #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
239
}