[R] ls() pattern question

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Jul 15 16:20:44 CEST 2021


Yes,. Thanks for the clarification. I tried to indicate this with my
comment about it being used as the "name" argument of ls().
What I should have said is that it is syntactically wrong as a way to
specify an argument of any function -- "myarg != something" does not
specify anything about the argument "myarg" of a function not equalling
something --  unless, as you pointed out, it positionally matches to
"myarg" and evaluates to something. But that was certainly not the OP's
intent, and I thought it was worth pointing out that he appeared to be
confused about the syntax of argument specification in general.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jul 15, 2021 at 2:09 AM Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:

> On 14/07/2021 8:21 p.m., Bert Gunter wrote:
> > Actually fun( param != something..) is syntactically incorrect in the
> first
> > place for any function!
>
> You have to be careful with absolute statements:
>
>    > f <- function(pattern) cat("It's legal!")
>    > f(pattern != something..)
>    It's legal!
>
> "pattern != something.." is a legal expression, which would return a
> logical vector.  My function didn't ever evaluate it, so the fact that
> variables named "pattern" and "something.." didn't exist in the global
> env didn't make any difference.  It's legal syntax, just wrong.
>
> Duncan Murdoch
>
> >
> > ls sees "pat != whatever"  as the "name" argument of ls() and can't make
> > any sense of it, of course.
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Wed, Jul 14, 2021 at 5:01 PM Andrew Simmons <akwsimmo using gmail.com>
> wrote:
> >
> >> Hello,
> >>
> >>
> >> First, `ls` does not support `!=` for pattern, but it's actually
> throwing a
> >> different error. For `rm`, the objects provided into `...` are
> substituted
> >> (not evaluated), so you should really do something like
> >>
> >> rm(list = ls(pattern = ...))
> >>
> >> As for all except "con", "DB2", and "ora", I would try something like
> >>
> >> setdiff(ls(), c("con", "DB2", "ora"))
> >>
> >> and then add `rm` to that like
> >>
> >> rm(list = setdiff(ls(), c("con", "DB2", "ora")))
> >>
> >> On Wed, Jul 14, 2021 at 7:41 PM Kai Yang via R-help <
> r-help using r-project.org>
> >> wrote:
> >>
> >>> Hello List,
> >>> I have many data frames in environment.  I need to keep 3 data frames
> >>> only, con DB2 and ora.
> >>> I write the script to do this.
> >>> rm(ls(pattern != c("(con|DB2|ora)")))
> >>>
> >>>
> >>> but it give me an error message:
> >>>
> >>>
> >>> Error in rm(ls(pattern != c("(con|DB2|ora)"))) :
> >>>    ... must contain names or character strings
> >>>
> >>> I think the pattern option doesn't support != ? and is it possible to
> fix
> >>> this?
> >>> Thank you,
> >>> Kai
> >>>
> >>>
> >>>
> >>>          [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide
> >>> http://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >>
> >>          [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list