2

I want R to load a certain file on initialization, so naturally I added a source command to my Rprofile so:

  .First <- function()
   {
         cat("\n   Welcome to R MotherFnorder!!!\n\n")
         setwd("/home/username/Code/R/")
       source("/home/username/Code/R/file.R")
   }

But now when I start R it throws a 'function not found' error for default functions like runif or rnorm. When I load the same file manually into the workspace I get no errors.

Ixxie
  • 1,393
  • 1
  • 9
  • 17
  • 2
    It is because you try to use functions not already imported in R, because your script is run... first. –  Jun 17 '14 at 07:40
  • Aha..... that kinda makes sense. So is there another function that is run after everything else in the beginning of the session? – Ixxie Jun 17 '14 at 09:28

2 Answers2

1

You don't need (or, really, want) to create a .First . If you put those lines into your .Rprofile they'll execute just fine. -- With the proviso @Pascal pointed out, that any function called in your file.R must have its library loaded first. So, near the bottom of your .Rprofile, just put

library(whatever_packages_needed)
cat("\n   Welcome to R MotherFnorder!!!\n\n")
setwd("/home/username/Code/R/")
source("/home/username/Code/R/file.R")

EDIT: I cannot reproduce your problem. I added these lines to the end of my .Rprofile:

#testing SO problem with libloading
library(stats)
runif(10)

And the console returns ten nice numbers.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Thank you for your answer, but unfortunately your solution didn't work. I get exactly the same error. {I already have the libraries used in the file called in its header; regardless, I added them again in the `.Rprofile` just to check, and it didn't make a difference.} – Ixxie Jun 18 '14 at 06:43
  • I tried the code mention in the updated notes, and this very same code also works for me. It seems this problem is specifically an issue with the way `source()` function is called in this context. – Ixxie Jun 18 '14 at 12:43
  • BUT it turns out what IS the problem is that I simply didn't have `library(stats)` in the header of my file. I assumed those functions were native to R I guess. Thanks for the help! – Ixxie Jun 18 '14 at 12:49
1

The reason for the error is that when .First() the packages are not yet loaded.

Although runif and rnorm might seem like default functions, they are actually part of the stats package. And as such, they are NOT available when .First() is called (unless you specifically call that package from within .First)

... which also explains this:

When I load the same file manually into the workspace I get no errors.

After .First() but before you have the chance to run anything manually, the default packages are attached. And hence available to your functions when you call it manually.


The solution is to create a file (if it does not already exist) called "~/.Rprofile" and put in there the lines you currently have in .First()

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178