SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Main Changes:
  2. #    A) use library(shinyFiles) and library(fs) instead of base R's file.choose() because
  3. #       running file.choose() fails in a non-Rstudio environment like jupyter.
  4. #       In jupyter, can set the default port/host first with either of:
  5. #            options(shiny.port=5001, shiny.host="0.0.0.0", shiny.launch.browser=FALSE); paneleditor_GUI()
  6. #            paneleditor_GUI(launch.browser=FALSE, port=5001, host='0.0.0.0')
  7. #    B) While we're at it, created a new button which allows (re)choosing folder to trigger refreshing the panel
  8. #            which has the side effect of allowing repeated use of the gui without needing to restart
  9.  
  10. render_paneleditor_ui <- function(working.directory, ...) {renderUI({
  11.     fluidPage(
  12.         fluidRow(
  13.             column(6,
  14.                 textInput("paneleditorui_output_folder", label = "Output folder name", value = "renamed")
  15.             ),
  16.             column(3,
  17.                 actionButton("paneleditorui_process_files", "Process files")
  18.             )
  19.         ),
  20.         fluidRow(
  21.             column(12,
  22.                     rhandsontable::rHandsontableOutput("paneleditorui_panel_table")
  23.             )
  24.         )
  25.     )
  26. })}
  27.  
  28.  
  29. get_panel_table <- function(files.list) {
  30.     message("Reading FCS parameters...")
  31.     panel.table <- premessa:::read_parameters(files.list)
  32.     message("Done")
  33.     common.names <- premessa:::get_common_names(panel.table)
  34.     problem.idx <- premessa:::get_problem_idx(panel.table, common.names)
  35.     panel.table <- panel.table[, order(colSums(problem.idx), decreasing = T), drop = F]
  36.  
  37.     panel.table <- data.frame(Parameter = row.names(panel.table), common.names, panel.table, check.names = F, stringsAsFactors = F)
  38.     names(panel.table)[2] <- "Most common"
  39.     return(panel.table)
  40. }
  41.  
  42.  
  43.  
  44. shinyServer(function(input, output, session) {
  45.    
  46.     volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), shinyFiles::getVolumes()())
  47.     shinyFiles::shinyDirChoose(input, "directory", roots = volumes, session = session)
  48.    
  49.     output$directorypath <- renderPrint({
  50.         shinyFiles::parseDirPath(volumes, input$directory)
  51.     })
  52.    
  53.     working.directory <- NULL
  54.     panel.table <- NULL
  55.    
  56.     observeEvent(input$directory, {
  57.         working.directory <- shinyFiles::parseDirPath(volumes, input$directory)
  58.          
  59.         if( length(working.directory) > 0 && dir.exists(working.directory) ) {
  60.            
  61.             output$paneleditorUI <- render_paneleditor_ui(working.directory)
  62.            
  63.             files.list <- list.files(working.directory, pattern = "*.fcs$", ignore.case = T)
  64.             files.list <- file.path(working.directory, files.list)
  65.  
  66.             panel.table <- get_panel_table(files.list)
  67.  
  68.             showModal(modalDialog(
  69.                 title = "Panel editor report",
  70.                 sprintf("FCS files ingested from: %s", file.path(working.directory))
  71.             ))
  72.            
  73.             #moved rhandsontable invocation here to (re)trigger upon valid working.directory
  74.             output$paneleditorui_panel_table <- rhandsontable::renderRHandsontable({
  75.                 temp <- panel.table
  76.  
  77.                 for(i in 1:ncol(temp))
  78.                     temp[, i][is.na(temp[, i])] <- "absent"
  79.  
  80.                 df <- data.frame(Remove = FALSE, temp, check.names = F, stringsAsFactors = F)
  81.  
  82.  
  83.                 hot <- rhandsontable::rhandsontable(df, rowHeaderWidth = 100)
  84.                 hot <- rhandsontable::hot_cols(hot, fixedColumnsLeft = 3, renderer = "
  85.                     function(instance, td, row, col, prop, value, cellProperties) {
  86.                         if(col == 0)
  87.                             Handsontable.renderers.CheckboxRenderer.apply(this, arguments)
  88.                         else {
  89.                             Handsontable.renderers.TextRenderer.apply(this, arguments)
  90.                             if(instance.params != null) {
  91.                                 if(instance.params.data[row][0])
  92.                                     td.style.background = 'lightgrey'
  93.                                 else {
  94.                                     if(value == 'absent')
  95.                                         td.style.background = 'orange'
  96.                                     else if(value != instance.params.data[row][2] && col > 2)
  97.                                         td.style.background = 'lightpink'
  98.                                 }
  99.                             }
  100.                         }
  101.                         return(td)
  102.                     }"
  103.                 )
  104.                 hot
  105.             })
  106.             #
  107.            
  108.         }
  109.     })
  110.  
  111.  
  112.     observeEvent(input$paneleditorui_process_files, {
  113.         working.directory <- shinyFiles::parseDirPath(volumes, input$directory)
  114.         message(paste("writing to", file.path(working.directory, input$paneleditorui_output_folder)))
  115.  
  116.         if( ! is.null(input$paneleditorui_process_files) &&
  117.             input$paneleditorui_process_files != 0 &&
  118.             length(working.directory) > 0 &&
  119.             dir.exists(working.directory) ) {
  120.  
  121.                 df <- rhandsontable::hot_to_r(input$paneleditorui_panel_table)
  122.                 for(i in 1:ncol(df))
  123.                     df[, i] <- gsub("absent", NA, df[, i])
  124.                 df$Remove <- as.logical(df$Remove)
  125.  
  126.                 panel.table$"Most common" <- df$"Most common" <- NULL
  127.  
  128.                 showModal(modalDialog(
  129.                     title = "Panel editor report",
  130.                     "File processing started, please wait..."
  131.                 ))
  132.  
  133.                 premessa:::rename_parameters_in_files(working.directory, input$paneleditorui_output_folder, df)
  134.  
  135.                 showModal(modalDialog(
  136.                     title = "Panel editor report",
  137.                     sprintf("File processing completed. The output files are located in: %s", file.path(working.directory, input$paneleditorui_output_folder))
  138.                 ))
  139.  
  140.         }
  141.     })
  142.  
  143. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top