Henrik Bengtsson
2003-May-30 14:40 UTC
[R] How to check if a pipe was successfully opened or not?
Is there a way to detect if the opening of a connection to a pipe was successful or not? Here are two examples # Works > con <- pipe("ls") > res <- open(con, open="r") > print(res) NULL # Does not work > con <- pipe("unknown_command") > res <- open(con, open="r") > 'unknown_command' is not recognized as an internal or external command, operable program or batch file. > print(res) NULL Can I make my script recognize/detect that the latter failed? try() will not catch the error. The error message is not written to stdout so sink() won't "catch" it either. Does anyone know of a (cross-platform) way to test if "unknown_command" exists or not on the current system before calling pipe()/open()? I'm running R v1.7.0 on WinXP. Thanks Henrik Bengtsson Dept. of Mathematical Statistics @ Centre for Mathematical Sciences Lund Institute of Technology/Lund University, Sweden (Sweden +2h UTC, Melbourne +10 UTC, Calif. -7h UTC) +46 708 909208 (cell), +46 46 320 820 (home), +1 (508) 464 6644 (global fax), +46 46 2229611 (off), +46 46 2224623 (dept. fax) h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/
Prof Brian Ripley
2003-May-30 15:28 UTC
[R] How to check if a pipe was successfully opened or not?
On Fri, 30 May 2003, Henrik Bengtsson wrote:> Is there a way to detect if the opening of a connection to a pipe was > successful or not? Here are two examples > > # Works > > con <- pipe("ls") > > res <- open(con, open="r") > > print(res) > NULL > > # Does not work > > con <- pipe("unknown_command") > > res <- open(con, open="r") > > 'unknown_command' is not recognized as an internal or external > command, > operable program or batch file. > > print(res) > NULLopen() always returns NULL.> Can I make my script recognize/detect that the latter failed? try() will > not catch the error. The error message is not written to stdout so > sink() won't "catch" it either. Does anyone know of a (cross-platform) > way to test if "unknown_command" exists or not on the current system > before calling pipe()/open()? > > I'm running R v1.7.0 on WinXP.The C code called by open() has fp = popen(con->description, mode); if(!fp) { warning("cannot open cmd `%s'", con->description); return FALSE; } so presumably your system's popen is returning a FILE stream even though the command cannot be opened. Not much we can do about that. Solaris says The popen() function returns a null pointer if files or processes cannot be created. but I think the problem is that the `process' is that launching the shell, not that of the command. I am not using Windows, so cannot check there. -- 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
Apparently Analagous Threads
- Ctrl-C with R.exe and Rterm.exe in R v1.9.0
- Suggestion to modify "Writing R Extensions"
- Webcuts - Navigating HTML help using only the keyboard
- R CMD INSTALL is sensitive to trailing newlines in DESCRIPTION.in
- Is it ok to use the name on a bundle and one if its packages?