[R] scripts with littler

John Lawrence Aspden news at aspden.com
Tue Jan 9 22:46:41 CET 2007

Jeffrey Horner wrote:
>> John Lawrence Aspden wrote:
>>> I'm actually tempted to use
>> #!/usr/bin/env r
>> rm(list=ls()[ls()!="argv"])
> Eww!! I'm not sure you want to do that. I would recommend sticking with:
> #!/usr/bin/r -v
> as that gives you a "truer" scripting environment. I understand that
> won't load the libraries in your home area automatically, but consider
> the way scripts in other languages are written and distributed: they
> usually load the libraries at the beginning of the script. Silently
> loading them before the script is run hides behavior from the script user.
> If you have libraries installed outside of the library search path,
> consider expanding it with .libPaths() before calling library() or
> require().
> Cheers,
> Jeff

Hi, thanks, it's not that it doesn't load the libraries automatically (which
I'd hate), it's that it no longer knows how to load them.

I've got a library (brainwaver), installed locally in ~/R/library, and this
information is recorded in the ~/.Renviron file.

This is because there's no debian package for it, and I don't want to mess
up the system by trying to install it manually as root (after all, it
should be fairly obvious that I don't know what I'm doing!...)

In my script I load the library, but if I call it using
#!/usr/bin/r --vanilla, this stops working.

(I can still load the system-wide libraries, it's the ones installed in my
home directory that break)

Since I can't use subroutines without using the library mechanism, and I
want to use brainwaver, and I want people to be able to use this stuff
without needing root privileges, or needing to hack hard-coded file
locations into every script, I'd prefer ~/.Renviron read.

Also, of course, using #!/usr/bin/r depends on it being installed there, and
I can't use the env mechanism and still pass it the vanilla option.

My main problem with R's/littler's default behaviour is that it introduces
lots of spurious variables pulled in from .Rdata that are different
depending where it's invoked.

I'm aware that the rm(list... is a nasty hack, but it seems like the least
bad option. Most of the other things seem to produce scripts that won't
work if you tar them up and send them to people.

Thanks for the hint about .libPaths(), but without ~/.Renviron how am I to
know which directories to add to it?

Of course I'm not saying that there might not be other subtle difficulties
with the default. But so far I prefer the default + explicitly remove all
variables to --vanilla, and I can't pass --vanilla without being sure where
R's installed anyway!

Is anyone still reading by this point?? Thanks for your perseverance if so!

Cheers, John.

Contractor in Cambridge UK -- http://www.aspden.com

More information about the R-help mailing list