R/app_server.R

Defines functions app_server

#' @import shiny
app_server <- function(input, output,session) {
  state <- reactiveValues(lopo = NULL,
                          lopo_path = NULL)
  
  # When app initialises, make and load a temp copy of the example sqliteDB
  # This gives full demo functionality of CRUD capabilities, but updates will not persist.
  observeEvent(TRUE, {
    cat(file=stderr(), "Running intial lopo read\n")
    s_path <- "inst/example_lopo/BP40657.sqlite"
    file.copy(from = s_path, to = tempdir(), overwrite = TRUE)

    lopo <- getLopo("BP40657", file.path(tempdir(), "BP40657.sqlite"))
    lopo$CRUD <- "Edit - Delete"
    lopo
    state$lopo <- lopo
    state$lopo_path <- file.path(tempdir(), "BP40657.sqlite")
  }, ignoreNULL = TRUE)
  
  # Call modules
  callModule(m_home_server, "home1")
  callModule(m_overview_server, "overview1")
  lopo_requested_updates <- callModule(m_lopo_server, "lopo1", lopo_info = state)
  
  # All of the DB handling should be moved into a separate module
  # TODO: Not sure best approach
  observe({
    req(lopo_requested_updates())
    # browser()
    study = "BP40657"
    query = lopo_requested_updates()
    query <- stringr::str_replace(query, "study", study)
   
    cat(file=stderr(), "User update made, synching with DB\n")
    cat(file=stderr(), query, "/n")
   
    con <- dbConnect(RSQLite::SQLite(),  state$lopo_path)
    dbSendQuery(con, query)
    
    request <- paste0("SELECT * FROM ", study)
    res <- unname(dbSendQuery(con,request))
    lopo <- dbFetch(res)
    dbClearResult(res)
    dbDisconnect(con)
    
    lopo$CRUD <- "Edit - Delete"
    state$lopo <- lopo

    # TODO Maintain connection whilst app runs and close connection on shiny exit.
    # onStop(function() {
    #   dbDisconnect(con)
    # })
  })
}
kismet303/lopo3000 documentation built on Dec. 5, 2019, 8:40 a.m.