app.R

library(shiny)
library(DT)
library(tidyverse)
library(cerberus)
library(leaflet)

ui <- fluidPage(
    
    # App title ----
    titlePanel("Cerberus quality control program"),
    
    # Sidebar layout with input and output definitions ----
        sidebarLayout(
    
            # Sidebar panel for inputs ----
            sidebarPanel(
            
    # Upload zip files
    fileInput("file", "Upload SHARK generated Zip file", accept = ".zip"),
    # action button to unzip the file
    actionButton("unzip", "Unzip Files"),
    # action button to view the file
    actionButton("view", "Viewer"),
    # Input: Choose datatype ----
    selectInput("dataset", "Choose a datatype:",
                choices = c("check_datatype",
                            "check_Chlorophyll",
                            "check_Epibenthos",
                            "check_EpibenthosDropvideo",
                            "check_GreySeal",
                            "check_HarbourPorpoise",
                            "check_HarbourSeal",
                            "check_PhysicalChemical",
                            "check_Phytoplankton",
                            "check_Picoplankton",
                            "check_PrimaryProduction",
                            "check_RingedSeal",
                            "check_SealPathology",
                            "check_Sedimentation",
                            "check_Zoobenthos",
                            "check_Zooplankton")),
    # action button to display map the data
    actionButton("map", "Map"),
    # action button to analyze the file
    actionButton("analyze", "QC"),
    # Download Button
    downloadButton("downloadreport", "Download QC report"),
    br(),
    # Map of data
    leafletOutput("mapdisplay")
    
        ),
    
    # Main panel for displaying outputs ----
    mainPanel(
        img(src='https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/SMHI_Logo.svg/200px-SMHI_Logo.svg.png', align = "right"),
        # Output: Tabset w/ plot, summary, and table ----
        tabsetPanel(type = "tabs",
                    tabPanel("Overview", 
                             # to display the metadata of the zipped file
                             tableOutput("filedf"),
                             # to display the list of unzipped files
                             tableOutput("zipped"),
                             # to display the table of the file to be analyzed
                             DT::dataTableOutput("datatable")),
                    tabPanel("Check Fields", tableOutput("datatype")),
                    tabPanel("Check On Land", tableOutput("onland")),
                    tabPanel("Check Depth", tableOutput("depth")),
                    tabPanel("Check Outliers", tableOutput("outliers")),
                    tabPanel("Match Taxa", tableOutput("taxa"))
        )
    
    )
    )
)

# server()

server <- function(input, output, session) {
    
    # Reactive value for selected dataset ----
    datasetInput <- reactive({
        switch(input$dataset,
               "check_datatype"=check_datatype,
               "check_Chlorophyll"=check_Chlorophyll,
               "check_Epibenthos"=check_Epibenthos,
               "check_EpibenthosDropvideo"=check_EpibenthosDropvideo,
               "check_GreySeal"=check_GreySeal,
               "check_HarbourPorpoise"=check_HarbourPorpoise,
               "check_HarbourSeal"=check_HarbourSeal,
               "check_PhysicalChemical"=check_PhysicalChemical,
               "check_Phytoplankton"=check_Phytoplankton,
               "check_Picoplankton"=check_Picoplankton,
               "check_PrimaryProduction"=check_PrimaryProduction,
               "check_RingedSeal"=check_RingedSeal,
               "check_SealPathology"=check_SealPathology,
               "check_Sedimentation"=check_Sedimentation,
               "check_Zoobenthos"=check_Zoobenthos,
               "check_Zooplankton"=check_Zooplankton)
    })
    
    output$filedf <- renderTable({
        if(is.null(input$file)){return ()}
        input$file # the file input data frame object that contains the file attributes
    })
    
    
    # Unzipping files on click of button and then rendering the result to dataframe
    observeEvent(input$unzip,
                 output$zipped <- renderTable({
                     unzip(input$file$datapath, list = TRUE, exdir = getwd())
                 })
                 
    )
    
    # View data table
    observeEvent(input$view,
                 output$datatable <- DT::renderDataTable({
                     readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1"))
                     
                     })
                 
    )
    
    # View map 
    observeEvent(input$map,
                 output$mapdisplay <- renderLeaflet({
                     plot_map_leaflet(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")))
                     
                 })
                 
    )
    
    # Perform QC analyses
    observeEvent(input$analyze,
                 output$datatype <- renderTable({
                     check_datatype(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")))

                 })
                 
    )
    observeEvent(input$analyze,
                 output$onland <- renderTable({
                     check_onland(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")),report=TRUE)
                     
                 })
                 
    )
    observeEvent(input$analyze,
                 output$depth <- renderTable({
                     check_depth(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")), report=TRUE)
                     
                 })
                 
    )
    observeEvent(input$analyze,
                 output$outlier <- renderTable({
                     check_outliers_dataset(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")), report=TRUE)
                     
                 })
                 
    )
    observeEvent(input$analyze,
                 output$taxa <- renderTable({
                     match_wormstaxa(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1"))$scientific_name, ask=FALSE)
                     
                 })
                 
    )
    
    # Downloadable file of report document ----
    #output$downloadreport <- downloadHandler(filename = paste(input$file$datapath,"_cerberus_QC_report.html",sep=""),
    #                                       content = report(readr::read_delim(unz(description = input$file$datapath, filename = "shark_data.txt"), delim ="\t",guess_max = 2000, col_names = T, locale = readr::locale(encoding = "latin1")))
    #    )
}

shinyApp(ui, server)
sharksmhi/tryout documentation built on Dec. 27, 2019, 5:34 a.m.