[R] print all variables inside function

William Dunlap wdunlap at tibco.com
Mon May 23 23:03:54 CEST 2016


If you really want to return all the objects in a function, I think it
is better to return as.list(environment()), perhaps adding the
all.names=TRUE argument to capture names starting with a dot.
I only have done this while debugging a function and then I
find it is more convenient to return just environment().

eval(parse(text="...")) will have problems when objects in the
environment have odd names, like 'Two words', '...', or '..1'.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, May 23, 2016 at 12:46 PM, Thierry Onkelinx <thierry.onkelinx at inbo.be
> wrote:

> Dear Jan,
>
> This will return a list with all objects from within the function.
>
> test <- function(){
>   a <- 10
>   b <- 3 * a + 1
>   x <- -1
>   output <- paste(objects(), objects(), sep = "=")
>   output <- paste(output, collapse = ",")
>   output <- paste("list(", output, ")")
>   return(eval(parse(text = output)))
> }
> test()
>
> Best regards,
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
> Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> 2016-05-23 21:26 GMT+02:00 Jan Kacaba <jan.kacaba at gmail.com>:
>
> > Hello dear R-help
> >
> > I would like to use some short and simple names multiple times inside
> > one script without collisions. I need to wrap the variables inside
> > some object. I know I can use class function or environment. For
> > example as follows:
> >
> > exmp1<-function(){
> >
> > ########
> > # knowns
> > pa=0.35
> > pb=0.35
> > pc=0.30
> > pad=0.015
> > pbd=0.010
> > pcd=0.020
> > ########
> >
> > ########
> > # unknowns
> > pd=pa*pad+pb*pbd+pc*pcd
> > pdc=pc*pcd/pd
> > pda=pa*pad/pd
> > pba=pb*pbd/pd
> > ########
> >
> > y<-c(pad=pad,pbd=pbd,pcd=pcd,pd=pd,pdc=pdc,pda=pda,pba=pba) # this
> > line I would like to automate so I don't have to write it every time
> > return(y)
> > }
> > output<-exmp1()
> >
> > Is it somehow possible to print 'Unknows' and 'Knowns' from exmp1
> > function without the need of explicitly write the 'y' line which puts
> > all variables inside list? For example with an imaginary function
> > 'fprint' which takes exmp1 as the input: fprint(exmp1).
> >
> > ______________________________________________
> > R-help at 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 at 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