eemR icon indicating copy to clipboard operation
eemR copied to clipboard

Allow eem_bind to accept a list of existing eem/eemlist objects

Open jpshanno opened this issue 5 years ago • 1 comments

Hi I just found eemR today and it's a great package!
I wanted to reading in a subset of files from a directory while testing it out. I ran into a problem after reading in multiple files with lapply I wanted to combine them into a single eemlist object. It looks like eem_bind is meant for the job, but it throws an error when a list of eems is passed in. I traced it back and when a list is passed to eem_bind it wraps it in another list, so the class check fails. Changing eem <- list(...) to eem <- c(...) in the first line of eem_bind takes care of it. The reprex below shows the error and the successful bind with eem <- c(...).
I also wrote an import function for a Horiba, and hope to submit it as a pull request in the future. I have to generalize it a little more because I think the file layout can change depending on the CSV export options selected.

library(eemR)

files <- 
  c(system.file("extdata/cary/scans_day_1/sample1.csv", package = "eemR"),
    system.file("extdata/cary/scans_day_1/sample2.csv", package = "eemR"))

eems <- 
  lapply(files,
         eem_read,
         import_function = "cary")

str(eems, max.level = 1)
#> List of 2
#>  $ :List of 1
#>   ..- attr(*, "class")= chr "eemlist"
#>  $ :List of 1
#>   ..- attr(*, "class")= chr "eemlist"

eem_bind(eems)
#> Error in eem_bind(eems): all(list_classes %in% c("eem", "eemlist")) is not TRUE

eem_bind2 <- 
  function(...) {
    eem <- c(...)
    
    
    list_classes <- unlist(lapply(eem, function(x) {
      class(x)
    }))
    
    stopifnot(all(list_classes %in% c("eem", "eemlist")))
    
    eem <- lapply(eem, eemR:::my_unlist)
    eem <- unlist(eem, recursive = FALSE)
    
    class(eem) <- "eemlist"
    
    return(eem)
  }

eem_bind2(eems)
#>    sample ex_min ex_max em_min em_max is_blank_corrected is_scatter_corrected
#> 1 sample1    220    450    230    600              FALSE                FALSE
#> 2 sample2    220    450    230    600              FALSE                FALSE
#>   is_ife_corrected is_raman_normalized
#> 1            FALSE               FALSE
#> 2            FALSE               FALSE

identical(eem_bind(eems[[1]], eems[[2]]),
          eem_bind2(eems))
#> [1] TRUE

Created on 2020-01-14 by the reprex package (v0.3.0)

jpshanno avatar Jan 14 '20 20:01 jpshanno

Hi there. Good catch. I am up for a PR whenever you want 👍

PMassicotte avatar Jan 14 '20 20:01 PMassicotte