R/app_server.R

Defines functions app_server

#' The application server-side
#' 
#' @param input,output,session Internal parameters for {shiny}. 
#'     DO NOT REMOVE.
#' @import shiny dplyr purrr shinyFiles readr stringr
#' @noRd
app_server <- function( input, output, session ) {
  
  volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
 
  shinyDirChoose(input, "folder", roots = volumes, session = session, restrictions = system.file(package = "base"))
 
  
  output$selected_folder <- renderPrint({
    if (is.integer(input$folder)) {
      cat("No folder has been selected")
    } else {
      parseDirPath(volumes, input$folder)
    }
  })
  
  observeEvent(input$go, {
    if(is.integer(input$folder)){
      showNotification('No date folder selected', type = 'error')
    } else if(is.null(input$mv2nm)){
      showNotification('No conversion input', type = 'error')
    } else {
      
      req(not_null(input$folder), not_null(input$mv2nm))
      date_folder <- shinyFiles::parseDirPath(volumes, input$folder)
      mv2nm <- as.numeric(input$mv2nm)
    
    
      if(is.na(mv2nm)){
        showNotification('Not a valid conversion number - Only use numbers.', type = 'error')
      } else {
    
        observation_folders <- list.files(date_folder, full.names = T) %>% 
          magrittr::extract(str_detect(., "obs"))
        
        all_files <-  list.files(date_folder, recursive = TRUE, full.names = T)
        
        grouped4r_files <- all_files %>%
          magrittr::extract(str_detect(., "grouped"))
        
        directions <- all_files %>%
          magrittr::extract(str_detect(., "directions"))
        
        
        read_directions <- suppressMessages(read_csv(directions)) %>%
          mutate(folder = observation_folders,
                 grouped_file = grouped4r_files,
                 baseline_start_sec = as.numeric(baseline_start_sec)*5000,
                 baseline_stop_sec = as.numeric(baseline_stop_sec)*5000,
                 mv2nm = mv2nm)%>%
          filter(include == "yes")
      
        
        processed_data <- pmap(read_directions, process)
        
        walk2(processed_data, read_directions$folder, ~write_tsv(as.data.frame(.x), 
                                                                 path = paste0(.y, '/processed.txt'),
                                                                 col_names = FALSE))
        
      
        showNotification('Processing Complete', type = 'message')
      }
    }

  })
  
  
}
brentscott93/processr documentation built on June 10, 2020, 12:34 a.m.