[Rd] system() in packages
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Apr 27 18:14:22 CEST 2007
You have missed the point completely.
There is no way you can use the same command on a Unix-alike and Windows,
so using system() in packages always needs to be conditionalized on OS.
If you want to use a shell on Windows, use shell(): you can even use bash
with it (but startup will be really slow on Windows): the sh.exe in the
Rtools toolkit is much lighter weight.
On Fri, 27 Apr 2007, Tony Plate wrote:
> With cygwin bash installed under Windows, one can use pipes in system(),
> e.g., like this:
>
> R 2.5.0 under Windows XP:
> > system("echo foo | sed s/foo/bar/") # this doesn't work under windows
> foo | sed s/foo/bar/
> > # but using 'bash -c' does:
> > system("bash -c \"echo foo | sed s/foo/bar/\"")
> bar
> > # and some use of quotes within the command actually works!
> > system("bash -c \"echo foo | sed 's/foo/bar rrr/'\"")
> bar rrr
> >
>
> [Beware: complex quoting inside the -c command can be difficult to get
> right.]
>
> -- Tony Plate
>
>
> Prof Brian Ripley wrote:
>> The first comment is that will only work on Unix-alikes, since '|' needs a
>> shell.
>>
>> So, if this is on a Unix-alike you need to establish if the program is in
>> the path at run time and cache the result. I have no idea if this would
>> actually work, but for example system('gp --version') might provide a
>> suitable test.
>>
>> On Fri, 27 Apr 2007, Robin Hankin wrote:
>>
>>> Hello
>>>
>>> Quite often I need higher precision, or larger numbers,
>>> than IEEE double precision allows. One strategy I sometimes
>>> use is to use system() to call pari/gp, which is not constrained by
>>> IEEE.
>>>
>>> [pari/gp is a GPL high-level mathematical programming language
>>> geared towards pure mathematics]
>>>
>>> Two of my packages contain lines like the following:
>>>
>>>> system(" echo '1.12^66' | gp -q f",intern=TRUE)
>>> [1]
>>> "1771.697189476241729649767636564084681203806302318041262248838950177194
>>> 116346432205160921568393661760"
>>>
>>> Note the high precision of the answer.
>>>
>>> My question is, how to deal with the possibility that pari/gp is not
>>> installed?
>>>
>>> If the system cannot find gp for some reason, I get:
>>>
>>>> system(" echo '1.12^66' | gp -q f",intern=TRUE)
>>> sh: line 1: gp: command not found
>>> character(0)
>>> What's the recommended way to handle this eventuality gracefully? The
>>> functions that do use pari/gp have "pure" R equivalents (but much
>>> slower and less accurate) so I want users to be able to install the
>>> package without pari/gp.
>>>
>>>
>>> --
>>> Robin Hankin
>>> Uncertainty Analyst
>>> National Oceanography Centre, Southampton
>>> European Way, Southampton SO14 3ZH, UK
>>> tel 023-8059-7743
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
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, UK Fax: +44 1865 272595
More information about the R-devel
mailing list