R/app_server.R

Defines functions app_server

#' @import shiny
app_server <- function(input, output, session) {
  state <- reactiveValues(study = NULL,
                          reporting_activity = NULL,
                          lopo = NULL,
                          lopo_path = NULL,
                          pgm_dir = NULL,
                          output_dir = NULL,
                          output_name = NULL,
                          output_path = NULL,
                          output_pgm_path = NULL,
                          github_url = 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)
    file.copy(from = "inst/program", to = tempdir(), recursive = 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")
    state$pgm_dir <- file.path(tempdir(), "program")
    
    # TO DO: make this dynamic
    report_name <- "t_ae.R"
    # report_path <- file.path("inst", "program", "t_ae.R")
    report_path <- file.path(state$pgm_dir, report_name)
    state$output_name <- report_name
    state$output_pgm_path <- report_path
    state$study <- "Demo Study"
    state$reporting_activity <- "Demo CSR"
    state$output_path <- file.path(tempdir(), "outputs", "t_ae.pdf")
    
  }, ignoreNULL = TRUE)
  
 
  
  # Call modules
  callModule(m_home_server, "home1", state = state)
  callModule(m_overview_server, "overview1", state = state)
  lopo_requested_updates <- callModule(m_lopo_server, "lopo1", state = state)
  callModule(mod_output_review_server, "review1", state = state, report_path = report_path)
  
  # 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)
    # })
  })
  
  # Table of app_state for developing/debugging
  output$state_table <- renderTable({
    state <- reactiveValuesToList(state) 
    state$lopo <- NULL
    df <- 
      tibble::enframe(state) %>% 
      tidyr::unnest(keep_empty = TRUE, cols=c(value))
  })
}
kismet303/lopo3000 documentation built on Dec. 15, 2019, 12:31 a.m.