## Data 

### Citizen Air - laadt je eigen metingen op {data-height=400}

```{r}
output[["citizenair-example.xls"]] <- downloadHandler(
  filename = function() "citizenair-example.xls",
  content = function(file) file.copy(from = system.file(package = "citizenair", "data-raw", "citizenair-example.xls"), file)
)
output[["citizenair-blank.xls"]] <- downloadHandler(
  filename = function() "citizenair-blank.xls",
  content = function(file) file.copy(from = system.file(package = "citizenair", "data-raw", "citizenair-blank.xls"), file)
)

tags$blockquote("Deze toepassing laat je toe om de kwaliteit van je eigen luchtmeting in te schatten. 
                Hiervoor moet je je data plaatsen in het template dat je via",  
                tags$a(href="https://hoemeetiklucht.eu", "www.hoemeetiklucht.eu"), 
                "kan samenstellen.")
tags$ul(
  tags$li("Download een voorbeeld excel file: ", 
          downloadLink("citizenair-example.xls", "citizenair-example.xls")),
  tags$li("Download een lege excel file waarin je je data kan copy-pasten: ", 
          downloadLink("citizenair-blank.xls", "citizenair-blank.xls")),
  tags$li("Ga naar de handleiding op ", 
          tags$a(href="https://hoemeetiklucht.eu", "hoemeetiklucht.eu"))
)
```

```{r}
fileInput("uiInput_xl", label = "Selecteer het bestand met uw metingen: ", accept = "application/excel",
          buttonLabel = div("excel: ", icon("file-excel-o")), placeholder = "Nog geen bestand geselecteerd")
#materialSwitch(inputId = "uiInput_addexcel", label = "Combineer excel met vorige excel", value = FALSE, status = "info")
```

### Meetstations van de overheid  {data-height=500}

```{r}
leafletOutput("uiOutput_stations")
output$uiOutput_stations <- renderLeaflet({
  userdata <- citizenair_userdata()
  meta     <- userdata$appdata$getCitizenData("meta")
  stations <- APPDATA$getStations()
  region   <- APPDATA$getStationsArea()
  map <- leaflet(data = region)
  map <- addTiles(map)
  map <- addPolygons(map, weight = 3, fillOpacity = 0)
  map <- addCircleMarkers(map = map, data = stations, radius = 5, opacity = 0.3, fillOpacity = 1, weight = 3, 
                          popup = as.character(mapply(id = stations$id, label = stations$label, phenomena = stations$phenomena_label, 
                                         FUN=function(id, label, phenomena){
                                           sprintf("<b>Station %s: %s</b> <br>Meetwaardes:<br> %s", id, label, paste(sprintf(" - %s ", phenomena), collapse = "<br>"))
                                           })))
  if(!is.na(userdata$nearest_station)){
    stations <- subset(stations, id %in% userdata$nearest_station)
    stations <- head(stations, 1)
    map <- addPopups(map, data = stations, popup = sprintf("Dichtste officieel meetstation<br>%s: %s", stations$id, stations$label))
  }
  if(nrow(meta) > 0){
    map <- addPopups(map, lng = meta$lon, lat = meta$lat, popup = sprintf("<b>Sheet %s: %s</b>", meta$sheet_id, meta$sheet))
    map <- addCircleMarkers(map, lng = meta$lon, lat = meta$lat, color = "red", 
                            popup = sprintf("Sheet: %s\nDevice: %s", meta$sheet, meta$device), 
                            radius = 5, weight = 3, fillOpacity = 1)
  }
  
  map
})
```



## Overall {data-width=150}


### Opgeladen meetpunten

```{r}
renderValueBox({
  userdata <- citizenair_userdata()
  n <- nrow(userdata$appdata$getCitizenData("meta"))
  if(n > 0){
    valueBox(n, caption = "Uw meetpunten", icon = "fa-thumbs-up", color = "green")
  }else{
    valueBox(n, caption = "Uw meetpunten", color = NULL)
  }
})
```

### Dichtste officieel meetstation

```{r}
renderValueBox({
  userdata <- citizenair_userdata()
  stations <- userdata$appdata$getStations()
  stations <- subset(stations, id %in% userdata$nearest_station)
  if(nrow(stations) > 0){
    txt <- stations$label
    txt <- strsplit(txt, "-")[[1]]
    txt <- trimws(txt)
    txt <- HTML(paste(txt, collapse = "<br>"))
    vb <- valueBox(txt, caption = "Dichtste officieel meetstation")  
  }else{
    vb <- NULL
  }
  vb
})
```

```{r, eval=FALSE}
actionBttn("uiInput_showdata", label = "Toon je opgeladen data", block = TRUE, icon = icon("table"), size = "sm")
eventReactive(input$uiInput_showdata, {
  showModal(popups$cleaned_data)
})
```

### Polluenten door u gemeten

```{r}
renderText({
  userdata <- citizenair_userdata()
  userdata$appdata$getCitizenData("phenomena")
})
```

### Meetperiode

```{r}
renderTable({
  userdata <- citizenair_userdata()
  x <- as.data.table(userdata$appdata$getCitizenData("timeseries"))
  if(nrow(x) > 0){
    x <- x[, list(start = as.character(min(date, na.rm=TRUE)),
                  stop = as.character(max(date, na.rm=TRUE)))]
    x <- as.data.frame(x)  
  }else{
    x <- NULL
  }
  x
})
```

### Frequentie van uw metingen (in minuten)

```{r}
renderTable({
  userdata <- citizenair_userdata()
  x <- as.data.table(userdata$appdata$getCitizenData("timeseries"))
  if(nrow(x) > 0){
    x$sheet_id <- as.integer(x$timeseries_id)
    x <- x[, meetfrequentie := as.numeric(difftime(time, shift(time, type = "lag", n = 1L), units = "mins")), by = list(sheet_id)]
    x <- x[, list(n = .N), by = list(sheet_id, meetfrequentie)]
    x <- x[, pct := round(100 * n / sum(n), 1), by = list(sheet_id)]
    x <- subset(x, !is.na(meetfrequentie))
    x <- x[order(n, decreasing=TRUE), ]
    x <- merge(x, userdata$appdata$getCitizenData("meta"), by = "sheet_id")
    x <- as.data.frame(x)  
    x <- x[!duplicated(x$sheet_id), c("sheet_id", "device", "meetfrequentie")]
    x$meetfrequentie <- sprintf("elke %s minuten", round(x$meetfrequentie, 2))
  }else{
    x <- NULL
  }
  x
})
```
