[R] print all variables inside function

Duncan Murdoch murdoch.duncan at gmail.com
Mon May 23 21:40:22 CEST 2016


On 23/05/2016 3:26 PM, Jan Kacaba wrote:
> 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).

Why create them first?  Just do something like this:

knowns <- c(
  pa=0.35
  pb=0.35
  pc=0.30
  pad=0.015
  pbd=0.010
  pcd=0.020)

Duncan Murdoch



More information about the R-help mailing list