Skip to content

Instantly share code, notes, and snippets.

@jpshanno
Created June 13, 2018 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpshanno/dce0507be9a9dd155f73aa435b4d49b9 to your computer and use it in GitHub Desktop.
Save jpshanno/dce0507be9a9dd155f73aa435b4d49b9 to your computer and use it in GitHub Desktop.
Shiny DataViz For Multi-Sensor Campbell Data File
# This can be run in Rstudio and will display in the Viewer tab with a file input box.
# As it is written it only reads in Campbell Scientific *.dat files, but changing line 34 would solve that.
library(dplyr)
library(lattice)
library(miniUI)
library(readr)
library(shiny)
library(shinyjs)
library(tidyr)
check_data <- function() {
ui <- miniPage(
miniContentPanel(
scrollable = FALSE,
uiOutput("filePrompt"),
plotOutput("plot"),
div(
align = "center",
uiOutput("dateSlider"))
)
)
server <- function(input, output, session) {
data <- reactiveValues(
selected = NULL
)
observe({
if(is.null(input$fileIn)) return(NULL)
data$selected <- read_csv(input$fileIn$datapath,
skip = 4,
col_names = names(read_csv(input$fileIn$datapath,
skip = 1,
n_max = 1))) %>%
gather(key = sensor,
value = measurement,
contains("_"))
})
output$filePrompt <- renderUI({
if(is.null(data$selected)){
div(align = "center",
fileInput(inputId = "fileIn",
label = "Select a CS Logger File",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv",
".dat")),
actionButton("cancel",
"Cancel"))
}
})
output$dateSlider <- renderUI({
if(is.null(input$fileIn)) return(NULL)
div(
selectInput(inputId = "sensor",
label = "Select a Sensor",
choices = c("All", sort(unique(data$selected$sensor)))),
sliderInput(inputId = "dateRange",
label = NULL,
min = min(data$selected$TIMESTAMP),
max = max(data$selected$TIMESTAMP),
value = c(min(data$selected$TIMESTAMP),
max(data$selected$TIMESTAMP))),
actionButton(inputId = "done",
label = "Done"))
})
output$plot <- renderPlot({
if(is.null(input$fileIn)) return(NULL)
plotData <-
data$selected %>%
filter(TIMESTAMP >= input$dateRange[1],
TIMESTAMP <= input$dateRange[2])
if(input$sensor != "All"){
plotData <-
plotData %>% filter(sensor == input$sensor)
}
xyplot(measurement ~ TIMESTAMP | sensor,
data = plotData,
type = "l",
aspect = 0.6,
between = list(y = 1),
xlab = NULL,
ylab = "Measured Value",
strip = strip.custom(bg = "white",
par.strip.text = list(cex = 0.65)),
lattice.options = list(
layout.heights=list(bottom.padding=list(x=0), top.padding=list(x=0)),
layout.widths=list(left.padding=list(x=0), right.padding=list(x=0))
),
scales = list(y = list(relation = "free"),
x = list(format = "%m/%y"),
alternating = FALSE,
cex = 0.65))
})
observeEvent(input$done, {
stopApp()
})
observeEvent(input$cancel, {
stopApp()
})
}
runGadget(ui,
server)
}
check_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment