[R] print all variables inside function

Thierry Onkelinx thierry.onkelinx at inbo.be
Mon May 23 21:46:56 CEST 2016


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 op 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 op 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