Startup script in Rprofile throws 'function not found' errors - r

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.

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.

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()

Related

Don't print the name of the directory to stdout when using here::set_here('path')

I'm using the R package 'here' to define my working directory using the following command at the start of my script:
here::set_here(path='/path/to/my_directory/', verbose = F)
Every time I run the script it prints this to the console:
here() starts at /path/to/my_directory
Is there a way to suppress this output? I tried using the invisible() function but that didn't work...
The message you’re seeing is printed when you’re attaching the ‹here› package. Simply don’t do that (it’s unnecessary anyway) to prevent it.
Otherwise, load it as follows:
suppressPackageStartupMessages(library(here))
… yeah, not exactly elegant.

safe way of including a function in R environment

I have wrote a bunch of functions for my project for users to invoke interactively.
Now I'd like to have them safely included in the session.
I want to avoid a scenario where user types rm(list=ls()) and erases my functions from memory.
Initially I tried to save function to another environment and attach that environment to search path but along the way I changed something and R is not longer able to find my function.
My code is split among multiple files but snippet below ilustrates how are things organised, normally user will run app.R because it contains refernces to other files, configurations etc.:
./funs.R
id.mapping.env <- new.env(parent = emptyenv())
attach(id.mapping.env)
id.mapping.env$test_function<- function() {
print("It works")
}
./app.R
source("./funs.R")
test_function
If I run app.R I get error:
Error: could not find function "test_function"
Why won't R find my function? Do I have to resort to writing my own package to ensure all of my functions can be found?
I am not familiar with writing packages, if this is needed could you give me a tiny demo/tutorial(which I am googling right now).

Debugging package::function() although lazy evaluation is used

How can I debug efficiently in R if packages are unknown due to lazy evaluation. I would like to keep the basic browser() functionality as it works great - even with the testthat package. As explained in the following post, --with-keep.source is set for my project in "project options => Build Tools".
To reproduce the behavior, create a package TestDebug containing
myfun <- function(a,b) {return(a+b)}
and a script example.R
{
browser()
TestDebug::myfun(1,2)
}
Edit: The situation where TestDebug::myfun(1,2) calls otherpackage::myfun2(1,2) should be also covered. I think the situation should occur in every "real" package?
The following works for me.
I have my package TestDebug with my function
myfun <- function(a,b) {return(a+b)}
If I run the script
debug(TestDebug::myfun)
TestDebug::myfun(1,2)
The debugger steps right into the source of TestDebug::myfun() not into the :: function as it does when you place a browser() before the call to TestDebug::myfun(1,2).
As you mention in your question: in real-life situations TestDebug::myfun(1,2) often calls otherpackage::myfun2(1,2). If you try to step into otherpackage::myfun2(1,2) you will end up inside the :: function again.
To prevent this I add this functions called inside other functions to the debug index on the fly:
As soon as you are on the line inside TestDebug::myfun() where otherpackage::myfun2(1,2) is called I run debug(otherpackage::myfun2(1,2)) in the console. After that I can step into otherpackage::myfun2(1,2) without problems and end up in the source code of otherpackage::myfun2(1,2). (..and not in the source code of ::)
Don't forget to call undebug(otherpackage::myfun2(1,2)) after you're sure that your problem is not inside otherpackage::myfun2(1,2) to prevent the debugger to jump into otherpackage::myfun2(1,2) the next time it is called.
If you prefer you can also use debugonce(otherpackage::myfun(1,2)) (instead of debug(..)) to only debug a function once.

How to call R script from another R script, both in same package?

I'm building a package that uses two main functions. One of the functions model.R requires a special type of simulation sim.R and a way to set up the results in a table table.R
In a sharable package, how do I call both the sim.R and table.R files from within model.R? I've tried source("sim.R") and source("R/sim.R") but that call doesn't work from within the package. Any ideas?
Should I just copy and paste the codes from sim.R and table.R into the model.R script instead?
Edit:
I have all the scripts in the R directory, the DESCRIPTION and NAMESPACE files are all set. I just have multiple scripts in the R directory. ~R/ has premodel.R model.R sim.R and table.R. I need the model.R script to use both sim.R and table.R functions... located in the same directory in the package (e.g. ~R/).
To elaborate on joran's point, when you build a package you don't need to source functions.
For example, imagine I want to make a package named TEST. I will begin by generating a directory (i.e. folder) named TEST. Within TEST I will create another folder name R, in that folder I will include all R script(s) containing the different functions in the package.
At a minimum you need to also include a DESCRIPTION and NAMESPACE file. A man (for help files) and tests (for unit tests) are also nice to include.
Making a package is pretty easy. Here is a blog with a straightforward introduction: http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
As others have pointed out you don't have to source R files in a package. The package loading mechanism will take care of losing the namespace and making all exported functions available. So usually you don't have to worry about any of this.
There are exceptions however. If you have multiple files with R code situations can arise where the order in which these files are processed matters. Often it doesn't matter or the default order used by R happens to be fine. If you find that there are some dependencies within your package that aren't resolved properly you may be faced with a situation where a custom processing order for the R files is required. The DESCRIPTION file offers the optional Collate field for this purpose. Simply list all your R files in the order they should be processed to satisfy the dependencies.
If all your files are in R directory, any function will be in memory after you do a package build or Load_All.
You may have issues if you have code in files that is not in a function tho.
R loads files in alphabetical order.
Usually, this is not a problem, because functions are evaluated when they are called for execution, not at loading time (id. a function can refer another function not yet defined, even in the same file).
But, if you have code outside a function in model.R, this code will be executed immediately at time of file loading, and your package build will fail usually with a
ERROR: lazy loading failed for package 'yourPackageName'
If this is the case, wrap the sparse code of model.R into a function so you can call it later, when the package has fully loaded, external library too.
If this piece of code is there for initialize some value, consider to use_data() to have R take care of load data into the environment for you.
If this piece of code is just interactive code written to test and implement the package itself, you should consider to put it elsewhere or wrap it to a function anyway.
if you really need that code to be executed at loading time or really have dependency to solve, then you must add the collate line into DESCRIPTION file, as already stated by Peter Humburg, to force R to load files order.
Roxygen2 can help you, put before your code
#' #include sim.R table.R
call roxygenize(), and collate line will be generate for you into the DESCRIPTION file.
But even doing that, external library you may depend are not yet loaded by the package, leading to failure again at build time.
In conclusion, you'd better don't leave code outside functions in a .R file if it's located inside a package.
Since you're building a package, the reason why you're having trouble accessing the other functions in your /R directory is because you need to first:
library(devtools)
document()
from within the working directory of your package. Now each function in your package should be accessible to any other function. Then, to finish up, do:
build()
install()
although it should be noted that a simple document() call will already be sufficient to solve your problem.
Make your functions global by defining them with <<- instead of <- and they will become available to any other script running in that environment.

Make a user-created function in R

I'm sorry if this has been asked before but I can't find the answer.
Let's say I write a small function in R
add2<-function(a){
return(a+2)
}
I save it as add2.R in my home directory (or any directory). How do I get R to find it??
> add2(4)
Error: could not find function "add2"
I know I can open up the script, copy/paste it in the console, run it, and then it works. But how do I get it built-in so if I open and close R, it still runs without me copying and pasting it in?
One lightweight option:
dump("add2", file="myFunction.R")
## Then in a subsequent R session
source("myFunction.R")
An alternative:
save("add2", file="myFunction.Rdata")
## Then just double click on "myFunction.Rdata" to open
## an R session with add2() already in it
## You can also import the function to any other R session with
load("myFunction.Rdata")
Until you're ready to package up functions into your own private package, storing them in well organized, source()-ready text files (as in the 1st example above) is probably the best strategy. See this highly up-voted SO question for some examples of how experienced useRs put this approach into practice.
Before invoking the function (e.g. at the beginning of the script), you should source the file containing your user defined function/s, i.e. :
source("add2.R") # this executes add2.R script loading add2 function
Basically, source function executes the code included in the script passed as argument. So if the file contains only functions definitions it loads the function in memory for future use.
If you want to start it automatically, then you have to set-up startup script and then use one of the methods outlined in answers above.
/Library/Frameworks/R.framework/Versions/2.15/Resources/etc/
is(for mac) the location of Rprofile.site, which must be edited adequately.
My version of it is:
.First <- function()
{
dir='~/Desktop/Infobase/R/0_init/0_init.R'
if(file.exists(dir))
{
source(dir, chdir = TRUE)
} else {cat("startup file is not found at:",dir)}
cat("\nWelcome at", date(), "\n")
}
.Last <- function()
{
cat("\nGoodbye at ", date(), "\n")
}
Note, that after you have sourced 1 R script, you do not need to enter this file anymore. Just do all you need from the file you sourced. In my case file "0_init.R" contains no functions, it just contains loading of other scripts. Well, you've got the idea.
Also, if you are doing this I recommend you to store them in new environment. Actual environments are not really suitable for your own functions(They are better implemented if you have a package developed, otherwise you lose a lot of control).
use "attach", "detach", "search", etc....
attach(FUN,name="af2tr")

Resources