+ Reply to Thread
Results 1 to 10 of 10

Thread: Calling functions within functions in R

  1. #1
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Calling functions within functions in R




    I have been struggling with this for a while. I have two functions Func1(arg1) and Func2( arg2) in files File1.r and File2.r respectively.

    How do I call Func1(arg1) inside Func2. I have placed them in the same folder but when I call Func1 inside Func2 it gives error: object Func1 not found. I am not able to successfully use source function either.

  2. #2
    Points: 3,214, Level: 35
    Level completed: 10%, Points required for next Level: 136

    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Calling functions within functions in R

    As a R newbie, It seems that .RData and .Rhistory files may be used for data and function storage.

    How did you make Files1.r and File2.r?

  3. #3
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Re: Calling functions within functions in R

    I created Files1.r and wrote the Func1(arg1) in an R-editor. I do not understand your question.

    All I have is:
    Files1.r containing Func1(arg1)
    Files2.r containing Func2(arg2)

    and I make a call to Func2(arg2) inside Func1 and I am not successful in it. If you have separate questions about data and function storage please start a new thread else it puts the original thread in danger of going tangential.

  4. #4
    Beep
    Points: 61,011, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,042
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts
    You could place the functions in the same file. Then when you source that file both are loaded in and you don't have to worry about it?

    Quote Originally Posted by sak View Post
    If you have separate questions about data and function storage please start a new thread else it puts the original thread in danger of going tangential.
    It seems to me they were trying to help. The only question they asked was in relation to your original question.

  5. #5
    Points: 3,609, Level: 37
    Level completed: 73%, Points required for next Level: 41

    Location
    Norman, OK 73019
    Posts
    100
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Re: Calling functions within functions in R

    And how should I do it if the functions are in separate files. My functions are pretty big and I would like to keep them in separate files if possible.

  6. #6
    Beep
    Points: 61,011, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,042
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: Calling functions within functions in R

    You could put something like this in the definition of Func2:
    Code: 
    Func2 <- function(x, ...){ # whatever your functions declaration looks like...
      if(!exists("Func1")){
        source("C:/path/to/Func1/Func1.R")
      }
    
      # Rest of code
    }
    This will check if there is something defined as Func1 already and if there isn't then it will source in the file for Func1. This will prevent you from sourcing the file everytime you want to run Func2. The problems with this approach is that your function will break if you move the location of Func1.

    As an alternative you could create an R script that sources both Func2 and Func1 in and then you don't need to worry about modifying your current code and both will already be in the working environment.

    A third option is to create a local library and just use library to bring in ALL of your functions when you want to. The problems with this approach is that it takes a little bit more work if you ever change your functions because you'll need to repackage everything up.

  7. #7
    TS Contributor
    Points: 5,565, Level: 48
    Level completed: 8%, Points required for next Level: 185

    Location
    St Albans, UK
    Posts
    257
    Thanks
    0
    Thanked 7 Times in 5 Posts

    Re: Calling functions within functions in R

    The method I prefer is to initially load all the function scripts (e.g. function1.r) using the source function and then save them all as .Rdata files using the save function. When reqired the functions in the .Rdata file are then loaded using the load function which is much faster than loading R scipt files.

  8. #8
    Beep
    Points: 61,011, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,042
    Thanks
    260
    Thanked 2,135 Times in 1,816 Posts

    Re: Calling functions within functions in R

    If you have a lot of scripts in a folder you could use the following to source all of them in:
    Code: 
    # Replace this with the directory containing your functions
    j <- "~/Dropbox/Code/Rscripts"
    
    # Matches filenames of the form "something.R"
    idx <- grep("*\\.R$", dir(j), value = TRUE)
    
    # Loop through and source all the files
    for(i in idx){
      source(i)
    }
    
    ## Note that before doing that source you could
    ## test this loop by examining what is in idx
    idx
    
    # It should only contain the files you want to source.

  9. #9
    R purist
    Points: 20,319, Level: 89
    Level completed: 94%, Points required for next Level: 31
    TheEcologist's Avatar
    Location
    The Netherlands.
    Posts
    1,679
    Thanks
    224
    Thanked 439 Times in 249 Posts

    Re: Calling functions within functions in R

    @Sak, Dason method of;

    if(!exists("Func1")){
    source("C:/path/to/Func1/Func1.R")

    is what you want, note however that this loads the function into the evaluation environment, which is a temporary load, only accessible when your function is running. If you want global access to your function just use;

    source(file.choose()) # I'm assuming you use windoze

    And select your function. Use this before you run the other function and things should be fine. This code will open a windowed interface so you can browse and choose your code.

    Quote Originally Posted by jk90029 View Post
    As a R newbie, It seems that .RData and .Rhistory files may be used for data and function storage.

    How did you make Files1.r and File2.r?
    .Rhistory is just for saving your command history, its a glorified keylogger in R.
    Something saved as .Rdata is the preferred method for saving everything in your R-workspace from data, functions to environments.

    If you want to save functions the preferred method by far would be to take the selection of code that makes the file (not everything you have typed as with .Rhistory) and save it in a .R file (with e.g. notepad).
    That is what sak has done.
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  10. #10
    Points: 3,214, Level: 35
    Level completed: 10%, Points required for next Level: 136

    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Calling functions within functions in R


    Thanks to TheEcologist -

    Probably, .RHistory file is stored for possble future retyping of the command lines. We may save time tomorrow by storing command lines used today.

    Therefore, unless we use complicated and long command, .RHistory does NOT play an important role and therefore it may NOT be necessary to backup the .RHistory for possible future hard disk failure.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats