[R] more help needed Re: communicate from Rterm

stephen bond stenka1 at go.com
Fri Jan 18 19:01:01 CET 2008


I think the message from Prof. Ripley refers to calling shell and 
system from R. There is no "system" call in Windows cmd or VBA and I am 
calling R from VBA.

Now I want to use tryCatch to output the error to a text file and exit 
gracefully. My expression will be source("file.r") and that will 
contain file I/O and db connect+query. to exemplify I am trying with

tryCatch(read.csv(nosuchfile.csv),error=function(e){print
(signalCondition(e)},finally=print("graceful exit"))

and not only it does not work but prints me a warning, so handling 
definitely fails.

Please, help or refer to tryCatch examples with real handlers. I could 
not get much out of the examples in the help file.

Thanks everybody

----Original Message----
From: ripley at stats.ox.ac.uk
Date: 01/11/2008 12:31 
To: "Duncan Murdoch"<murdoch at stats.uwo.ca>
Cc: "stephen bond"<stenka1 at go.com>, <r-help at r-project.org>
Subj: Re: [R] communicate from Rterm

I think the confusion may be the assumption that Microsoft's use of 
'shell' has anything to do with 'shell' as used in the Unix/POSIX 
world.

For MinGW C executables (like Rterm.exe), redirection is expected to 
be 
handled by a shell, not by the executable.  (Other Windows runtimes 
may 
handle redirection themselves.)

Compare the R-for-Windows system() and shell() commands and you will 
see 
one way to do this.

On Fri, 11 Jan 2008, Duncan Murdoch wrote:

> On 1/11/2008 10:14 AM, stephen bond wrote:
>> I try with:
>>
>> ret = Shell(exestr)
>>
>> and it starts R, but the source file is not executed
>
> That's a VBA issue.  VBA isn't handling the input redirection "< m.
in.R
> > out.txt".  I don't know how to set that up:  you'll need to 
contact
> Microsoft tech support.
>
> Duncan Murdoch
>
>>
>> inside R
>>
>>> a=commandArgs()
>>> a
>> [1] "Rterm"  "--restore" "--save"  "--args" "2005-02-03" #correct 
date
>> passed
>> [6]  "<"  "m.in.R"  ">"  "out.txt"
>>>
>>
>> it restores the correct workspace, but then it fails to source the
>> script, which is located in the same dir as the workspace.
>>
>> inside R
>>
>>> source("m.in.R") # does the job, so it is not a finding problem
>>
>> BTW is it possible to get the error message from R somehow???
>>
>>
>> ----Original Message----
>> From: murdoch at stats.uwo.ca
>> Date: 01/11/2008 6:30
>> To: "stephen bond"<stenka1 at go.com>
>> Cc: <r-help at r-project.org>
>> Subj: Re: [R] communicate from Rterm
>>
>> On 1/11/2008 9:18 AM, stephen bond wrote:
>>> Please, help with announcing an error from Rterm:
>>>
>>> I am calling R from an Excel VBA and noticed that if there is an
>> error
>>> nothing conspicuous happens. I would like just a popup window when
>> R
>>> cannot finish cleanly. The "ret " value returned from Shell is
>> useless
>>> in determining what happened.
>>
>> This seems to be a VBA problem, not an R problem.  Rterm returns a 
0
>> return value when it exits cleanly, and a non-zero value when 
there's
>> an
>> error.  It looks as though you're losing that by wrapping the Rterm
>> call
>> in a batch file.  Can't VBA make the call to Rterm directly?
>>
>> Duncan Murdoch
>>
>>>
>>> Thank you all.
>>>
>>> *****************************
>>> Public Sub Repo()
>>> ds = InputBox("Type in reporting date as YYYY-MM-DD", "reporting
>>> date")
>>>
>>> ''-----   weird way to pass the date argument as Windows won't
>>> ''-----   execute the command line directly, so I create a .bat
>>>
>>> exestr = "Rterm --restore --save --args " & ds & " < m.in.R > out.
>> txt"
>>> Set fs = CreateObject("Scripting.FileSystemObject")
>>> Set a = fs.CreateTextFile("rpcd.bat", True)
>>> a.writeline (exestr)
>>> a.Close
>>>
>>> ret = Shell("rpcd.bat")
>>> MsgBox ("wait for about 5 seconds as the script runs
>> asynchronously")
>>>
>>> End Sub
>>>
>>> **************m.in.R************
>>> ds=commandArgs(T)[1]
>>>
>>> ans=f.repo(ds)
>>>
>>> q("yes")
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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 at r-project.org 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 at r-project.org 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.
>

-- 
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-help mailing list