[R] several functions in one *.R file in a R package

Jannis bt_jannis at yahoo.de
Thu Sep 8 11:28:30 CEST 2011


Thanks, Duncan, for your reply. You are right, the () in my code are not 
correct. Maybe my problem is that I do not really understand the exact 
effect of this dot . I have no tried with the following file in my /R 
folder in the package:


mainfunction<- function(x) {
    x2 <- .subfunction1(x)
    x3 <- .subfunction2(x2)
    x3
}

.subfunction1<- function(x) {
   x*2
}
.subfunction2<- function(x) {
   x*2
}

After I build the package and load it into R an run (for example):

mainfunction(2)

I get 8, which indicates that the functions are working. The reason that 
made me believe beforehand that the code is not working is that when I 
type the following to see the code of the function:

.subfunction1

or run:

.subfunction1(2)

I get an error that the object .subfunction1 can not be found. Is this 
the desired effect of adding the dot to the function name? Or do I do 
something wrong here?


Thanks
Jannis




On 09/06/2011 04:46 PM, Duncan Murdoch wrote:
> On 11-09-06 10:26 AM, Jannis wrote:
>> Dear list members,
>>
>>
>> i have build a package which contains a collection of my frequently 
>> used functions. To keep the code organized I have broken down some 
>> rather extensive and long functions into individual steps and bundled 
>> these steps in sub-functions that are called inside the main function.
>>
>> To keep an overview over which sub-function belongs to which main 
>> function I saved all the respective sub-functions to the same *.R 
>> file as their main-function and gave them names beginning with . to 
>> somehow hide the sub-functions. The result would be one *.R file 
>> in<package>/R for each 'main-function' containing something like:
>>
>>
>> mainfunction<- function() {
>>    .subfunction1()
>>    .subfunction2()
>>    #...
>> }
>>
>> .subfunction1()<- function() {
>>   #do some stuff
>> }
>> .subfunction2()<- function() {
>>   #do some more stuff
>> }
>>
>>
>> According to the way I understood the "Writing R Extensions" Manual I 
>> expected this to work. When I load the package, however, I get the 
>> error message that the sub-functions could not be found. Manually 
>> sourcing all files in the<package>/R directory however yields the 
>> expected functionality.
>>
>> In what way am I mistaken here? Any ideas?
>
> Those definitions of .subfunction1 and .subfunction2 are not 
> syntactically correct:  extra parens.  If that's just a typo in the 
> message, then you'll have to show us real code.  What you describe 
> should work.
>
> Duncan Murdoch
>



More information about the R-help mailing list