Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. targetf<-function(x=1,...){
  2. print(paste("x =",x))
  3. }
  4.  
  5. wrapperfX<-function(x,y,...)
  6. {
  7. ...<-if(missing(x){
  8. list()
  9. }else{
  10. list(x=x)
  11. }
  12. targetf(...)
  13. }
  14.  
  15. libfoo<-function(par1=1,...)
  16. {
  17. if (missing(par1))
  18. {
  19. warning("par1 is missing!!")
  20. }
  21. print(paste0('par1: ',par1))
  22. libbar(...)
  23. }
  24.  
  25.  
  26. libbar<-function(par2=1)
  27. {
  28. if (missing(par2))
  29. {
  30. warning("par2 is missing!!")
  31. }
  32. print(paste0('par2: ',par2))
  33. }
  34.  
  35.  
  36. libfoo2<-function(par3=1,...)
  37. {
  38. if (missing(par3))
  39. {
  40. warning("par3 is missing!!")
  41. }
  42. print(paste0('par3: ',par3))
  43. libbar2(...)
  44. }
  45.  
  46. libbar2<-function(par4=1)
  47. {
  48. if (missing(par4))
  49. {
  50. warning("par4 is missing!!")
  51. }
  52. print(paste0('par4: ',par4))
  53. }
  54.  
  55. wrapfun<-function(x,par1=3,...,par3,par4)
  56. {
  57. libfoo(par1,...) #With dots everything is simple
  58.  
  59. pars<-list()
  60. if(!missing(par3))
  61. {
  62. pars<-c(pars,par3=par3)
  63. }
  64. if(!missing(par4))
  65. {
  66. pars<-c(pars,par4=par4)
  67. }
  68. do.call(libfoo2,pars) #This is how we can pass specific arguments, and respecting missings properly.
  69. }
  70.  
  71. wrapfun(par1=5,par2=5,par3=5,par4=5)
  72. # [1] "par1: 5"
  73. # [1] "par2: 5"
  74. # [1] "par3: 5"
  75. # [1] "par4: 5"
  76.  
  77.  
  78. wrapfun()
  79. # [1] "par1: 3"
  80. # [1] "par2: 1"
  81. # [1] "par3: 1"
  82. # [1] "par4: 1"
  83. # Warning messages:
  84. # 1: In libbar(...) : par2 is missing!!
  85. # 2: In (function (par3 = 1, ...) : par3 is missing!!
  86. # 3: In libbar2(...) : par4 is missing!!
  87.  
  88. wrapfun(par4=5)
  89. # [1] "par1: 3"
  90. # [1] "par2: 1"
  91. # [1] "par3: 1"
  92. # [1] "par4: 5"
  93. # Warning messages:
  94. # 1: In libbar(...) : par2 is missing!!
  95. # 2: In (function (par3 = 1, ...) : par3 is missing!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement