Search code examples
rparameter-passingsubsetphyloseq

Passing argument to subset() and unique()


I am using the phyloseq package.

test <- function( ...){

bar <- unique(sampleData[,'pH'])

foo <- subset_samples(phyloseqObject, pH == as.numeric(bar[1]@.Data))
print(foo)

}

test(pH)

I want to pass pH as an argument to test() but unique() won't accept it as valid. I can pass 'pH' to test() but subset_samples() won't accept that as valid. I have tried coercing the argument to several different types with no luck.

SORCE for subset_samples:

subset_samples <- function(physeq, ...){
    if( is.null(sample_data(physeq)) ){ 
        cat("Nothing subset. No sample_data in physeq.\n")
        return(physeq)
    } else {
        oldDF <- as(sample_data(physeq), "data.frame")
        newDF <- subset(oldDF, ...)
        if( class(physeq) == "sample_data" ){
            return(sample_data(newDF))
        } else {
            sample_data(physeq) <- sample_data(newDF)
            return(physeq)
        }
    }
}

Solution

  • Building on what @desc said I managed to solve it like this:

    test <- function(...){
    
        bar <- unique(sampleData[,...])
    
        foo <- subset_samples(phyloseqObject, eval(parse(bar@names)) == as.numeric(bar[1]))
        print(foo)
    
    }
    
    test('pH')