I created a new project as a "R package" in RStudio v. 1.0.1.153. By default such a new R package project comes with file hello.R
containing a template function
hello <- function() {
print("Hello, world!")
}
which can be accessed simply by building and reloading the package (Ctrl+Shift+B), and then in another R session simply loading the built package and running the function:
> library(mylibrary)
> hello()
[1] "Hello, world!"
Now I would like to organize my functions to several files in the package. I add a new file methods.R
to the .\R\
sub-directory of the package with another function:
helloYouToo <- function() {
print("Hello you too!")
}
However, when I rebuild the package, and reload the library, I cannot access the function:
> library(mylibrary)
> helloYouToo()
Error in helloYouToo() : could not find function "helloYouToo"
I have a couple of questions. How should I
- divide the package functions into several files (not just single
hello.R
file) so that the files and the functions defined there are included into the package, and - what is the preferred way to access also within the package such functions which are defined within the same package but in another file (like in
methods.R
)?