Skip to content
Prev 367259 / 398506 Next

Make sure a data frame has been "fun through" a function

Sorry for not being clear. I have never used S3 methods before. Below is
some R code that sketches out my idea. Is this a sensible solution?

test_data <- data.frame(a=1:10, b=1:10, c=1:10)

functionA <- function(x, impossible_genotype){
    ##some data processing
    y <- x

    ##return S3 to be able to use impossible genotype later
    class(y) <- append(class(y),"genotypes")

    attr(y, "impossible_genotype") <- impossible_genotype

    return(y)
}

test_data_genotypes <- functionA(test_data, impossible_genotype="Ref")

functionB <- function(x){
    ##stop if pre-processed with functionA
    if(sum(class(x)=="genotypes")!=1){stop("Need to pre-process data with
functionA")}

    ##use this later in functionB to
    impossible_genotype <- attributes(x)$impossible_genotype

    alleles <- c("Ref", "Alt")

    coded_genotype <- alleles[alleles!=impossible_genotype]



    return(coded_genotype)
}

##stop if not pre-processed with functionA
functionB(test_data)

##processed with functionA
functionB(test_data_genotypes)

On Tue, Feb 21, 2017 at 6:41 AM, David Winsemius <dwinsemius at comcast.net>
wrote: