R/module_databaseSelector.R

Defines functions databaseSelector_server databaseSelector_UI

Documented in databaseSelector_server databaseSelector_UI

#' databaseSelector_UI
#'
#' @param id namespace
#'
#' @return modal ui
#' @export
#'
databaseSelector_UI <- function(id) {
  ns <- shiny::NS(id)
  
  tagList(
    uiOutput(ns("availableDB")),
    p("Location of experiment file:", align = "center"),
    verbatimTextOutput(ns("selectedSQLText"),
                       placeholder = TRUE)
  )
}


#' databaseSelector_server
#'
#' @param input  input 
#' @param output  output 
#' @param session session 
#' @param h3Label h3Label 
#' @param availableExperiments availableExperiments 
#' @param sqlDirectory sqlDirectory 
#'
#' @return filepath of selected database
#' @export
#'
databaseSelector_server <- function(input,
                                    output,
                                    session,
                                    h3Label = h3("First, select an experiment:"),
                                    availableExperiments,
                                    sqlDirectory){
  
  
  output$availableDB <- renderUI({
    ns <- session$ns
    selectInput(ns("selectExperiment"),
                label = h3Label,
                choices = c("None", availableExperiments$db),
                selected = "None",
                width = "50%"
    )
  })
  
  output$selectedSQLText <- renderPrint({
    
    fileNames <- tools::file_path_sans_ext(list.files(sqlDirectory$sqlDirectory,
                                                      pattern = ".sqlite",
                                                      full.names = FALSE))
    filePaths <- list.files(sqlDirectory$sqlDirectory,
                            pattern = ".sqlite",
                            full.names = TRUE)
    
    filePaths[which(fileNames == input$selectExperiment)]
    
  })
 
   userDBCon <- reactive({
    
    req(!is.null(input$selectExperiment))
    req(nchar(input$selectExperiment) > 0)
    req(input$selectExperiment != "None")
    validate(need(length(input$selectExperiment) == length(sqlDirectory$sqlDirectory), 
                  "databaseTabServer: userDBCon, createPool inputs are different lengths."))
    
    # pool will create a new sqlite if one doesn't exist, so let's stop that from happening here:
    req(file.exists(file.path(sqlDirectory$sqlDirectory, 
                              paste0(input$selectExperiment, ".sqlite"))))
    
    z <- tempRepo::createPool(fileName = input$selectExperiment,
                         filePath = sqlDirectory$sqlDirectory)[[1]]
    
    
    q <- c("IndividualSpectra",
           "XML",
           "metaData",
           "version" )
    
    req(all(q %in%  DBI::dbListTables(z)))
    
    return(z)
    
    
    
   })  
   
   return(list(userDBCon = userDBCon,
               inputs = input))
}
chasemc/tempRepo documentation built on April 8, 2019, 10:22 p.m.