Re: [R] scripts with littler
John Lawrence Aspden wrote: I've got a library (brainwaver), installed locally in ~/R/library, and this information is recorded in the ~/.Renviron file. In my script I load the library, but if I call it using #!/usr/bin/r --vanilla, this stops working. (Various private e-mails exchanged. Again, thanks Dirk!) Just in case anyone else is trying to do this, it turns out that if you can persuade your end users to install the library to ~/R/library, then you can say: #!/usr/bin/r --vanilla library(brainwaver, lib.loc='~/R/library') although in my case, brainwaver depends on another library, which it now can't find, so actually I have to load them in order: #!/usr/bin/r --vanilla library(waveslim, lib.loc='~/R/library') library(brainwaver, lib.loc='~/R/library') Alternatively, #!/usr/bin/r --vanilla .libPaths('~/R/library') library(brainwaver) works, although be careful, I've noticed that it seems to behave a bit strangely on my debian setup. e.g. #!/usr/bin/r --vanilla cat(.Library,'*', .libPaths(),\n) .libPaths('~/R/library') cat(.Library,'*', .libPaths(),\n) gives output /usr/lib/R/library * /usr/local/lib/R/site-library /usr/lib/R/site-library /usr/lib/R/library /usr/lib/R/library * ~/R/library /usr/lib/R/library that is, it seems to have removed /usr/local/lib/R/site-library and /usr/lib/R/site-library as well as added ~/R/library Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
[By now way off the subject line. Something like 'how to set the libraries inside an R session'.] On Wed, 10 Jan 2007, John Lawrence Aspden wrote: John Lawrence Aspden wrote: I've got a library (brainwaver), installed locally in ~/R/library, and this information is recorded in the ~/.Renviron file. In my script I load the library, but if I call it using #!/usr/bin/r --vanilla, this stops working. (Various private e-mails exchanged. Again, thanks Dirk!) Just in case anyone else is trying to do this, it turns out that if you can persuade your end users to install the library to ~/R/library, then you can say: #!/usr/bin/r --vanilla library(brainwaver, lib.loc='~/R/library') although in my case, brainwaver depends on another library, which it now can't find, so actually I have to load them in order: #!/usr/bin/r --vanilla library(waveslim, lib.loc='~/R/library') library(brainwaver, lib.loc='~/R/library') Alternatively, #!/usr/bin/r --vanilla .libPaths('~/R/library') library(brainwaver) works, although be careful, I've noticed that it seems to behave a bit strangely on my debian setup. 'It' (R) is behaving as you asked it to. Most likely you intended to ask for .libPaths(c(~/R/library, .libPaths())) e.g. #!/usr/bin/r --vanilla cat(.Library,'*', .libPaths(),\n) .libPaths('~/R/library') cat(.Library,'*', .libPaths(),\n) gives output /usr/lib/R/library * /usr/local/lib/R/site-library /usr/lib/R/site-library /usr/lib/R/library /usr/lib/R/library * ~/R/library /usr/lib/R/library that is, it seems to have removed /usr/local/lib/R/site-library and /usr/lib/R/site-library as well as added ~/R/library Exactly as documented. The argument is named 'new' and not 'add', BTW. Please 'be careful' in what you say about the work of others. Cheers, John. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
Brian Ripley wrote: Exactly as documented. The argument is named 'new' and not 'add', BTW. Please 'be careful' in what you say about the work of others. Agreed, no criticism intended. I really like R. Sorry. Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
Guys, thanks very much for your help. Rscript looks great and I'll look forward to it. The /usr/bin/env thing seems to be a general difficulty with the mechanism. One's first thought has to be to modify env to parse and then pass the arguments in the expected way (maybe #!/usr/bin/env2?), and of course one's second is that this must already have been done... An S of TFW produces 'Citizens for a better env', but this isn't as hopeful as it sounds. I'm actually tempted to use #!/usr/bin/env r rm(list=ls()) as the first two lines of every script, rather than messing about trying to pass options. Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
John Lawrence Aspden wrote: I'm actually tempted to use #!/usr/bin/env r rm(list=ls()) Ahem, it turns out to be better to use: #!/usr/bin/env r rm(list=ls()[ls()!=argv]) -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
John Lawrence Aspden wrote: John Lawrence Aspden wrote: I'm actually tempted to use #!/usr/bin/env r rm(list=ls()) Ahem, it turns out to be better 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 -- http://biostat.mc.vanderbilt.edu/JeffreyHorner __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
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 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] scripts with littler
Hi, I'm trying to write R scripts using littler (under Debian), and was originally using the shebang line: #!/usr/bin/env r However this picks up any .RData file that happens to be lying around, which I find a little disturbing, because it means that the script may not behave the same way on successive invocations. If you drop the /usr/bin/env trick then #!/usr/bin/r --vanilla seems to work, but it also prevents the loading of the libraries in my home directory, some of which I'd like to use. #!/usr/bin/r --no-restore doesn't work at all. Ideally I'd like #!/usr/bin/env r --no-restore Has anyone else been round this loop and can offer advice? Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] scripts with littler / subroutines
Hi (again), Another difficulty I'm having is creating a common function (foo, say) to share between two scripts. I've tried making a third file containing the function and then sourcing it with source (foo.R), but that only works if you run the script in the directory where foo.R is. (or if the scripts know where they're installed) The other solutions that occur are copy-and-paste, a preprocessor, or some sort of special-purpose library. I think I like the preprocessor best, but it's still kind of nasty. I have the feeling that I'm probably missing something obvious here! Can anyone help? Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
[John Lawrence Aspden] I'm trying to write R scripts using littler (under Debian), and was originally using the shebang line: #!/usr/bin/env r However this picks up any .RData file that happens to be lying around, which I find a little disturbing, because it means that the script may not behave the same way on successive invocations. If you drop the /usr/bin/env trick then #!/usr/bin/r --vanilla seems to work, but it also prevents the loading of the libraries in my home directory, some of which I'd like to use. #!/usr/bin/r --no-restore doesn't work at all. Ideally I'd like #!/usr/bin/env r --no-restore Has anyone else been round this loop and can offer advice? I usually do something like: #!/bin/sh R --slave --vanilla EOF R script goes here... EOF # vim: ft=r If you need to search special places for packages, you may tweak exported environment variables between the first and second line. -- François Pinard http://pinard.progiciels-bpi.ca __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler / subroutines
[John Lawrence Aspden] Another difficulty I'm having is creating a common function (foo, say) to share between two scripts. In your previous message, you were telling us that you want to load from your home directory. You might put the common functions there, maybe? -- François Pinard http://pinard.progiciels-bpi.ca __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
Thanks, that's a really neat mechanism, ( I especially like the note to vim, which will save all my scripts having to end .R ) Is there any way to get at the command line and stdio though? With littler I can do things like: #!/usr/bin/env r print(argv) t=read.table(file=stdin()) so that I can write unix-style filters. Cheers, John. François Pinard wrote: I usually do something like: #!/bin/sh R --slave --vanilla EOF R script goes here... EOF # vim: ft=r If you need to search special places for packages, you may tweak exported environment variables between the first and second line. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler / subroutines
François Pinard wrote: [John Lawrence Aspden] Another difficulty I'm having is creating a common function (foo, say) to share between two scripts. In your previous message, you were telling us that you want to load from your home directory. You might put the common functions there, maybe? I am doing at the moment, and using source to load them in. I'm worried about what happens when I come to distribute the code though. After a couple of e-mails off-list (thanks Dirk!) it sounds as though the solution is to create a library for R with the common subroutines in, and then load that from the scripts. Cheers, John. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
Looks like it will be possible to write scripts with R 2.5.0 using the new -f flag and file(stdin). From https://svn.r-project.org/R/trunk/NEWS : o Command-line R (and Rterm.exe under Windows) accepts the options '-f filename', '--file=filename' and '-e expression' to follow other script interpreters. These imply --no-save unless --save is specified. [..] o file(stdin) is now recognized, and refers to the process's 'stdin' file stream whereas stdin() refers to the console. These may differ, for example for a GUI console, an embedded application of R or if --file= has been used. On 1/8/07, John Lawrence Aspden [EMAIL PROTECTED] wrote: Thanks, that's a really neat mechanism, ( I especially like the note to vim, which will save all my scripts having to end .R ) Is there any way to get at the command line and stdio though? With littler I can do things like: #!/usr/bin/env r print(argv) t=read.table(file=stdin()) so that I can write unix-style filters. Cheers, John. François Pinard wrote: I usually do something like: #!/bin/sh R --slave --vanilla EOF R script goes here... EOF # vim: ft=r If you need to search special places for packages, you may tweak exported environment variables between the first and second line. -- Contractor in Cambridge UK -- http://www.aspden.com __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] scripts with littler
He missed o There is a new front-end Rscript which can be used for #! scripts and similar tasks. See help(Rscript) and 'An Introduction to R' for further details. and that is needed for #! scripts. (You cannot write #!/path/to/R -f as R is a shell script and so disallowed on most OSes.) As I understand the earlier question, if you have #! /usr/env cmd arg1 arg2 /usr/env is passed 'cmd arg1 arg2' as the name of the utility, at least under bash which says If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell executes the specified interpreter on operating systems that do not handle this exe- cutable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name ... Note the 'single'. This is detailed as part of the description of Rscript referred to in the NEWS item. I don't know how universal this is, but the Solaris Bourne shell does the same thing. François Pinard's idea of here documents is nice until you want to read from the script's stdin rather than the script itself. On Mon, 8 Jan 2007, Gabor Grothendieck wrote: Looks like it will be possible to write scripts with R 2.5.0 using the new -f flag and file(stdin). From https://svn.r-project.org/R/trunk/NEWS : oCommand-line R (and Rterm.exe under Windows) accepts the options '-f filename', '--file=filename' and '-e expression' to follow other script interpreters. These imply --no-save unless --save is specified. [..] ofile(stdin) is now recognized, and refers to the process's 'stdin' file stream whereas stdin() refers to the console. These may differ, for example for a GUI console, an embedded application of R or if --file= has been used. On 1/8/07, John Lawrence Aspden [EMAIL PROTECTED] wrote: Thanks, that's a really neat mechanism, ( I especially like the note to vim, which will save all my scripts having to end .R ) Is there any way to get at the command line and stdio though? With littler I can do things like: #!/usr/bin/env r print(argv) t=read.table(file=stdin()) so that I can write unix-style filters. Cheers, John. François Pinard wrote: I usually do something like: #!/bin/sh R --slave --vanilla EOF R script goes here... EOF # vim: ft=r If you need to search special places for packages, you may tweak exported environment variables between the first and second line. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595__ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.