self-written function does not work when it's started from package

54 Views Asked by At

I have written a function to automatically normalize my flow cytometry data using gaussNorm from the package flowstats. This function works just fine when I include it in my script. However, when I implement it in my package that I have prepared, It throws an error message saying 'cannot transform class flowSet into data frame'.

The code of my function looks like this:

flow_normalize2 <- function(df, id.channel, channels) {

  df <- as.data.frame(df)
  df <- df[,c(channels, id.channel)]
  if(!is.numeric(df[,id.channel])) {warning("Input dataframe must be purely numeric!")}


  ids <- df[,id.channel] %>% unique
  myflowdata <- lapply( ids, function(i) flowCore::flowFrame(as.matrix(df[df[,id.channel]==i,])) )

  fst <- flowCore::flowSet(myflowdata)
  transform.func <- logicleTransform()
  transform.list <- flowCore::transformList(channels, transform.func)
  fst <- transform(fst, transform.list)
  fst.norm <- flowStats::gaussNorm(fst, channel.names = channels, max.lms = 1)[[1]]
  fst.norm <- rbind(flowCore::fsApply(fst.norm, as.matrix, use.exprs = TRUE))
  fst.norm <- as.data.frame(fst.norm)

  id.verif <- paste0(id.channel,"norm")
  df[,channels] <- fst.norm[,channels]
  df[,id.verif] <- fst.norm[,id.channel]

  if(identical(df[,id.channel],df[,id.verif])) { print("gaussNorm normalization successful") }

  df[,id.verif] <- NULL

  return(df)

}

I have already tried to include the specifications of the package to use (i.e. flowCore and flowStats) but it still doesn't work when I try to start it from my package. Can someone please help?

0

There are 0 best solutions below