Extract function parameters from a R expression

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract function parameters from a R expression

Sigbert Klinke
Hi,

I have read an R program with

expr <- parse("myRprg.R")

How can I extract the parameters of a specifc R command, e.g. "library"?

So, if myprg.R containes the lines

library("xyz")
library("abc")

then I would like to get "xyz" and "abc" back from expr.

Thanks in advance

Sigbert

--
https://hu.berlin/sk



______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract function parameters from a R expression

hadley wickham
You need to recursively walk the parse tree/AST. See, e.g.,
https://adv-r.hadley.nz/expressions.html#ast-funs

Hadley

On Wed, Jun 20, 2018 at 10:08 AM, Sigbert Klinke
<[hidden email]> wrote:

> Hi,
>
> I have read an R program with
>
> expr <- parse("myRprg.R")
>
> How can I extract the parameters of a specifc R command, e.g. "library"?
>
> So, if myprg.R containes the lines
>
> library("xyz")
> library("abc")
>
> then I would like to get "xyz" and "abc" back from expr.
>
> Thanks in advance
>
> Sigbert
>
> --
> https://hu.berlin/sk
>
>
>
> ______________________________________________
> [hidden email] 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.
>



--
http://hadley.nz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract function parameters from a R expression

Bert Gunter-2
... or if the argument is just quoted text or a numeric value as in your
library() example, don't parse the text and use regex's to search for the
function call and pick out the text of the arguments.

Again, this only works (I think) for the simple sort of case of your
example. Beyond that, you'll have to follow Hadley's prescription.

Cheers,
Bert





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, Jun 20, 2018 at 1:26 AM, Hadley Wickham <[hidden email]> wrote:

> You need to recursively walk the parse tree/AST. See, e.g.,
> https://adv-r.hadley.nz/expressions.html#ast-funs
>
> Hadley
>
> On Wed, Jun 20, 2018 at 10:08 AM, Sigbert Klinke
> <[hidden email]> wrote:
> > Hi,
> >
> > I have read an R program with
> >
> > expr <- parse("myRprg.R")
> >
> > How can I extract the parameters of a specifc R command, e.g. "library"?
> >
> > So, if myprg.R containes the lines
> >
> > library("xyz")
> > library("abc")
> >
> > then I would like to get "xyz" and "abc" back from expr.
> >
> > Thanks in advance
> >
> > Sigbert
> >
> > --
> > https://hu.berlin/sk
> >
> >
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
>
>
> --
> http://hadley.nz
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract function parameters from a R expression

Gabor Grothendieck
In reply to this post by Sigbert Klinke
If you specifically want to know which packages were loaded by the script
then using a vanilla version of R (i.e. one where only base packages are
loaded):

  vanilla_search <- search()
  source("myRprg.R")
  setdiff(search(), vanilla_search)



On Wed, Jun 20, 2018 at 4:08 AM, Sigbert Klinke
<[hidden email]> wrote:

> Hi,
>
> I have read an R program with
>
> expr <- parse("myRprg.R")
>
> How can I extract the parameters of a specifc R command, e.g. "library"?
>
> So, if myprg.R containes the lines
>
> library("xyz")
> library("abc")
>
> then I would like to get "xyz" and "abc" back from expr.
>
> Thanks in advance
>
> Sigbert
>
> --
> https://hu.berlin/sk
>
>
>
> ______________________________________________
> [hidden email] 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.
>



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.