Alen
Alen

Reputation: 197

R inverted regex pattern for ls

In R I load one environment from a file that contains various timeseries plus one configuration object/vector.

I want to process all timeseries in the environment in a loop but want to exclude the configuration object.

At the moment my code is like this:

for(x in ls(myEnv)) {
    if(x!="configData") {

        # do something, e. g.
        View(myEnv[[x]], x)
    }
}

Is there a way to use the pattern parameter of the ls-function to omit the if clause?

for(x in ls(myEnv, pattern="magic regex picks all but *configData*")) {

    # do something, e. g.
    View(myEnv[[x]], x)
}

All examples I could find for pattern were based on a whitelist-approach (positive list), but I'd like to get all except configData.

Is this possible?

Thanks.

Upvotes: 2

Views: 155

Answers (1)

Alen
Alen

Reputation: 197

for( x in setdiff(ls(myEnv), "configData") ) 

and

for(x in grep("configData", ls(myEnv), value=TRUE, invert=TRUE))

both work fine, thanks.

BTW, cool! I wasn't aware of hiding it by using a leading "." ... so the best solution for me is to make sure that configData becomes .configData in the source file so that ls() won't show it.

Upvotes: 2

Related Questions