R/calculate_results.R

Defines functions calculate.results

#' Calculate results of (multiple) selections
#'
#' @param tree tree object representing Patchmaster data file
#' @param selections a list of selections (i.e. from shinyTree "get_selected(tree, format = "names")")
#' @param level which level of selections to operate on. 
#' If level=1, the selections are expected to represent experiments, however this is not tested. 
#' All series of the selected experiments are returned, regardless which Series are selected.
#' With level=2, only the selected experiments are evaluated. 
#'
#' @return A data frame with cursor results
#' @export
#'
calculate.results <- function(tree, selections, level = 2) {
  
  # check level of selected elements
  levels <-
    sapply(selections, function(x)
      length(attr(x, "ancestry")))
  
  # decide how to deal with different levels:
  # operate on the chosen level only
  sel2 <- selections[levels == level]
  
  
  
  if (level == 2) {
    # series selections
    return(do.call(rbind.fill, lapply(sel2, function(sel) {
      sel_ <- c(attr(sel, "ancestry"), sel)
      
      s<-getSeries(tree, sel_[1], sel_[2], sel_[3])
      PLXLSfiles<-attr(tree[[sel_[1]]], "PLXLSfiles")
      s$PLXLS_set_file(PLXLSfiles[[1]])
      s$results()
    })))
  }
  
  if (level == 1) {
    return(do.call(rbind.fill, lapply(sel2, function(sel) {
      sel_ <- c(attr(sel, "ancestry"), sel)
      exp<-tree[[sel_]]
      # treat all elements of list 'exp' as selected from tree
      do.call(rbind.fill, lapply(names(exp), function(ser){
        # get the path of the series element to brew a 'selection' 
        #sel_<- attr(ser,"path") # 
        sel_ <- c(sel_, ser)
        s<-getSeries(tree, sel_[1], sel_[2], sel_[3])
        PLXLSfiles<-attr(tree[[sel_[1]]], "PLXLSfiles")
        s$PLXLS_set_file(PLXLSfiles[[1]])
        s$results()
      }))
      
      
    })))
  }
  
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.