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
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
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
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.
[R] littler+dget+stdin - segmentation fault
Hi, I'm trying to write a series of pipes using littler, and I get the following behaviour: Sorry if I'm just doing something witless, I'm new to R. I'm using the latest versions from debian testing (2.4.0 and 0.0.8). $ r -e 'a-dget(file=stdin()); print(a)' ?list(a=2) Segmentation fault In R itself this works: dget(file=stdin()) ?list(a=2) $a [1] 2 As do (from the command line): $ cat foo list(a=2) $ r -e 'a-dget(file=foo); print(a)' $a [1] 2 and (using littler and scan instead of dget) $ r -e 'a-scan(file=stdin()); print(a)' Thanks in advance, 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] littler+dget+stdin - segmentation fault
Jeffrey Horner wrote: John Lawrence Aspden wrote: Hi, I'm trying to write a series of pipes using littler, and I get the following behaviour: $ r -e 'a-dget(file=stdin()); print(a)' ?list(a=2) Segmentation fault You've found a bug which has been fixed. Expect a new version 0.0.9 of littler later tonight from here: http://dirk.eddelbuettel.com/code/littler/ What exactly are you trying to accomplish? Hi Jeff, that's gratifying! Thanks. I'm trying to write some scripts to process the output from a brain scanner, using some routines which someone else wrote to use interactively in R. The idea is to hide R from the end-user so that as far as they're concerned it's just a normal unix filter. Indeed the end-user is probably going to be another program eventually. However the first process is a time consuming wavelet transform and correlation step, and then there are loads of possibilities for what to do with the result. so I want to be able to say: process1 brain.data processed.data (time consuming) to do the hard bit, and then e.g. process2 processed.data process3 processed.data | graphing-tool to produce various visualizations and statistics I'm using dput/dget to read and write the intermediate structure (a list of matrices). All the other methods seem to remember the structure's original name and then stick it in the global namespace, which I find annoying. I also like the fact that dput/dget work with text files, which means that one day I'll be able to drop in a replacement for process1 written in C to deal with anticipated huge data sets. This seems to be working well so far (I'm only prototyping to see if it's feasible), but I'm having to work around the bug by having the file as a command line argument rather than piping it in. 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.