[R] Adding a function with default parameters into the Rcmdr menu

Milan Bouchet-Valat nalimilan at club.fr
Fri Nov 23 13:01:23 CET 2012


Le vendredi 23 novembre 2012 à 03:17 +0100, vincent guyader a écrit :
> Hi everyone,
> 
> I made some tests with Rcmdr, to add a function with default parameters :
> 
> For example (very simple):
> 
> myfunction<-function(var="314"){
> print("hello")
> print(var)
> }
> 
> if I run myfunction() directly i see :
> 
> > myfunction()
> [1] "hello"
> [1] "314"
> 
> it's ok.
> 
> But if i edit de Rcmdr-menu.txt (in
> C:\Users\myname\Documents\R\win-library\2.15\Rcmdr\etc)
> and add :
> 
> menu    MyMenu        topMenu
> ""                                            ""
> ""          ""
> item    topMenu              cascade
> "MyTest"                                MyMenu             ""          ""
> item    MyMenu       command         "Test"
> myfunction                   ""          ""
> 
> and I put myfonction into a file Rcmdr-test.R in the same folder
> 
> I have a new button with a cascade menu, and myfunction is corectly
> sourced.. but :
> 
> 
> that's what append :
> 
> > library(Rcmdr)
> Loading required package: tcltk
> Loading Tcl/Tk interface ... done
> Loading required package: car
> Loading required package: MASS
> Loading required package: nnet
> Sourced: Rcmdr-test.r
> 
> 
> Rcmdr Version 1.9-2
> 
> If i use the Menu :
> 
> [1] "hello"
> [1] "%var"
> 
> ->there are %var instead off 314.
> > myfunction
> function(var="314"){
> print("hello")
> print(var)
> }
> 
> I think that I made a mistake but I dont know were. How can I use Rcmdr
> menu AND a default parameter ?
> 
> Can you help me?
I'm not sure how it's supposed to work in Rcmdr, but a very simple
workaround is to call from the menus a custom function defined like
this:
doMyfunction <- function() myfunction()

Another solution is to add "..." before "var" in the arguments list of
your function. That way, the first argument passed by Rcmdr will not
incorrectly be matched to "var".
myfunction <- function(..., var="314"){
    print("hello")
    print(var)
}


My two cents




More information about the R-help mailing list