R/fRawDataProc_UserInputs.R

Defines functions fRawDataProc_UserInputs

Documented in fRawDataProc_UserInputs

#' This Shiny application allows the user to select a routine for initial data processing as well as select from a list of sites that have available data.

#' @export
#' @title Initial user inputs and site selection for FluxSynthU data processing
#' @param db.and.sites a character vector containing site names and their associated database (e.g. 'FLX_US-NR1' or 'AMF_CA-Obs')
#' @param descrip a character string describing the current Shiny application (defaults to NULL)
#'
#' @importFrom shiny runApp fluidPage h3 h4 h5 hr em strong actionButton fluidRow column selectInput verbatimTextOutput tags reactiveVal observeEvent updateSelectInput renderPrint observe stopApp
#' @importFrom shinyTree shinyTree renderTree updateTree get_selected


# fRawDataProc_UserInputs
# Shiny app designed for the initial stages of file management and raw data processing

# Updated 201001 to have more flexibility with handling flux databases
# Now outputs user site selections with information about which database was selected (previous versions did not have this functionality)
fRawDataProc_UserInputs <- function(db.and.sites, descrip = NULL) {
  # First split 'db.and.sites' into different lists according to their respective database

  db.and.sites.list <- list(`NEON` = substr(db.and.sites[grep(pattern = '\\NEO', db.and.sites)], start = 5,stop = 8),
                            `AMERIFLUX` = substr(db.and.sites[grep(pattern = '\\AMF', db.and.sites)], start = 5,stop = 10),
                            `FLUXNET2015` = substr(db.and.sites[grep(pattern = '\\FLX', db.and.sites)], start = 5,stop = 10))



  tree1 <- lapply(structure(list(  'Export datafiles for REddyProc?' = 'export.files',
                                   'Export plotting files?'= list(
                                     'Export plotting data as .txt? (needed for Shiny QC)' = 'export.plot.data',
                                     'Export plots as .png? (16-panel; 1-6 MB per plot)'   =  'export.plots')), stopened = TRUE),   function(x) structure(x, stopened = TRUE) )

  runApp(list(
    ui = fluidPage(
      fluidPage(
        h3(strong(paste(descrip))),
        h4(strong('Select options for data processing routine:')),
        shinyTree("tree1", checkbox = TRUE, theme = "proton", themeIcons = FALSE),
        actionButton('reset1', 'Reset nodes'),
        hr(),
        h4(strong("Click to select sites for processing:")),
        h5(em("Hold 'command + click' to select multiple sites ('command + click' deselects sites as well) ")),
        fluidRow(column(3, selectInput("amf", "Select AmeriFlux site(s):",
                                       db.and.sites.list[["AMERIFLUX"]],
                                       multiple = TRUE, selectize = FALSE, size = 15)),
                 column(3, selectInput("flx", "Select FLUXNET2015 site(s):",
                                       db.and.sites.list[["FLUXNET2015"]],
                                       multiple = TRUE, selectize = FALSE, size = 15)),
                 column(3, selectInput("neo", "Select NEON site(s):",
                                       db.and.sites.list[["NEON"]],
                                       multiple = TRUE, selectize = FALSE, size = 15)),

        ),

        verbatimTextOutput("result"),
        actionButton('reset2', 'Reset site selection'),
        tags$button(
          id = 'close',
          type = "button",
          class = "btn action-button btn-large btn-primary",
          onclick = "setTimeout(function(){window.close();},500);",  # close browser
          "BEGIN DATA PROCESSING (click)"
        )
      )
    ),
    server = function(input, output, session) {
      treeSelection <- reactiveVal(list())

      output$tree1 = renderTree({
        tree1
      })


      observeEvent(input$reset1, {
        updateTree(session, "tree1", data = tree1)
        treeSelection(list())
      })

      observeEvent(input$tree1, {
        treeSelection(get_selected(input$tree1, format = "classid"))
      })


      observeEvent(input$reset2, {
        updateSelectInput(session, "amf", choices = db.and.sites.list[["AMERIFLUX"]])
        updateSelectInput(session, "flx", choices = db.and.sites.list[["FLUXNET2015"]])
        updateSelectInput(session, "neo", choices = db.and.sites.list[["NEON"]])
      })


      output$result <- renderPrint({
        paste(c(input$amf, input$flx, input$neo))
      })

      observe({
        tree.selection <- input$tree1
        site.selection <- c(input$neo, input$amf, input$flx)
        if (input$close > 0) stopApp(list(tree.selection, "AMF" = input$amf, "FLX" = input$flx, "NEO" = input$neo))
      })

    }
  )
  )
}
ksmiff33/FluxSynthU documentation built on Dec. 15, 2020, 10:29 p.m.