Quantcast

Capture output of install.packages (pipe system2)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Capture output of install.packages (pipe system2)

Jeroen Ooms.
Is there any way to capture output (both stdout and stderr) from
install.packages? Solutions like sink and capture.output don't work
because the install.packages calls out to system2 which is executed in
a separate process:

    test <- capture.output(install.packages("MASS"))

The system2 function does have arguments stdout and stderr but afaik
these cannot be controlled via install.packages. I'm a bit reluctant
to start rolling my own version of install.packages just for this
reason.

Is there any way to somehow grab this output? Or alternatively, is
there a way to make R pipe stdout and stderr from system2 in such a
way that they can be captured with sink or capture.output in the R
parent process?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Duncan Murdoch-2
On 13-09-23 2:17 PM, Jeroen Ooms wrote:

> Is there any way to capture output (both stdout and stderr) from
> install.packages? Solutions like sink and capture.output don't work
> because the install.packages calls out to system2 which is executed in
> a separate process:
>
>      test <- capture.output(install.packages("MASS"))
>
> The system2 function does have arguments stdout and stderr but afaik
> these cannot be controlled via install.packages. I'm a bit reluctant
> to start rolling my own version of install.packages just for this
> reason.
>
> Is there any way to somehow grab this output? Or alternatively, is
> there a way to make R pipe stdout and stderr from system2 in such a
> way that they can be captured with sink or capture.output in the R
> parent process?

See the recent thread
<https://mailman.stat.ethz.ch/pipermail/r-devel/2013-September/067552.html>
for an approach to this.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Simon Urbanek
Duncan,

On Sep 23, 2013, at 10:20 PM, Duncan Murdoch <[hidden email]> wrote:

> On 13-09-23 2:17 PM, Jeroen Ooms wrote:
>> Is there any way to capture output (both stdout and stderr) from
>> install.packages? Solutions like sink and capture.output don't work
>> because the install.packages calls out to system2 which is executed in
>> a separate process:
>>
>>     test <- capture.output(install.packages("MASS"))
>>
>> The system2 function does have arguments stdout and stderr but afaik
>> these cannot be controlled via install.packages. I'm a bit reluctant
>> to start rolling my own version of install.packages just for this
>> reason.
>>
>> Is there any way to somehow grab this output? Or alternatively, is
>> there a way to make R pipe stdout and stderr from system2 in such a
>> way that they can be captured with sink or capture.output in the R
>> parent process?
>
> See the recent thread <https://mailman.stat.ethz.ch/pipermail/r-devel/2013-September/067552.html> for an approach to this.
>

Can you, please, elaborate on how it is that relevant? The linked thread is talking about sink() which it irrelevant here (to quote the original e-mail "Solutions like sink and capture.output don't work because the install.packages calls out to system2 which is executed in a separate process:"). Something I missed?

Re original question: there is keep_outputs which allows to re-direct a copy of the output into a file - at least for the actual install part of it.
On top of my head for full capture I can only think of custom C code that will re-direct stderr/out FDs as desired (it's really trivial to re-direct to files, for dynamic capture it's a little more involved but there are examples that do that).

Cheers,
Simon

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Duncan Murdoch-2
On 13-09-23 6:34 PM, Simon Urbanek wrote:

> Duncan,
>
> On Sep 23, 2013, at 10:20 PM, Duncan Murdoch <[hidden email]> wrote:
>
>> On 13-09-23 2:17 PM, Jeroen Ooms wrote:
>>> Is there any way to capture output (both stdout and stderr) from
>>> install.packages? Solutions like sink and capture.output don't work
>>> because the install.packages calls out to system2 which is executed in
>>> a separate process:
>>>
>>>      test <- capture.output(install.packages("MASS"))
>>>
>>> The system2 function does have arguments stdout and stderr but afaik
>>> these cannot be controlled via install.packages. I'm a bit reluctant
>>> to start rolling my own version of install.packages just for this
>>> reason.
>>>
>>> Is there any way to somehow grab this output? Or alternatively, is
>>> there a way to make R pipe stdout and stderr from system2 in such a
>>> way that they can be captured with sink or capture.output in the R
>>> parent process?
>>
>> See the recent thread <https://mailman.stat.ethz.ch/pipermail/r-devel/2013-September/067552.html> for an approach to this.
>>
>
> Can you, please, elaborate on how it is that relevant? The linked thread is talking about sink() which it irrelevant here (to quote the original e-mail "Solutions like sink and capture.output don't work because the install.packages calls out to system2 which is executed in a separate process:"). Something I missed?

Brian Ripley's reply describes how it is done in the tools package.  For
example, as I sent privately to Jeroen,

x <- system2("Rscript", "-e \"install.packages('MASS',
repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)

captures all of the output from installing MASS.  As Jeroen pointed out,
that isn't identical to running install.packages() in the current
session; a real version of it should fill in more of the arguments, not
leave them at their defaults.

Duncan Murdoch



> Re original question: there is keep_outputs which allows to re-direct a copy of the output into a file - at least for the actual install part of it.
> On top of my head for full capture I can only think of custom C code that will re-direct stderr/out FDs as desired (it's really trivial to re-direct to files, for dynamic capture it's a little more involved but there are examples that do that).
>
> Cheers,
> Simon
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

hadley wickham
> Brian Ripley's reply describes how it is done in the tools package.  For
> example, as I sent privately to Jeroen,
>
> x <- system2("Rscript", "-e \"install.packages('MASS',
> repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)
>
> captures all of the output from installing MASS.  As Jeroen pointed out,
> that isn't identical to running install.packages() in the current session; a
> real version of it should fill in more of the arguments, not leave them at
> their defaults.

It does seems a little crazy that you're in a R process, then open
another one, which then opens a 3rd session! (often indirectly by
calling R CMD install which then calls an internal function in tools)

Hadley

--
Chief Scientist, RStudio
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Paul Gilbert-2

On 13-09-23 08:20 PM, Hadley Wickham wrote:

>> Brian Ripley's reply describes how it is done in the tools package.  For
>> example, as I sent privately to Jeroen,
>>
>> x <- system2("Rscript", "-e \"install.packages('MASS',
>> repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)
>>
>> captures all of the output from installing MASS.  As Jeroen pointed out,
>> that isn't identical to running install.packages() in the current session; a
>> real version of it should fill in more of the arguments, not leave them at
>> their defaults.
>
> It does seems a little crazy that you're in a R process, then open
> another one, which then opens a 3rd session! (often indirectly by
> calling R CMD install which then calls an internal function in tools)

It does seem very much more straight forward to do this in the process
above R:

   R --vanilla --slave -e "install.packages('whatever',
    repo='http://cran.r-project.org')" >R.out  2>&1

(Omit mailer wrap.) Your mileage may vary depending on your OS.

Paul

>
> Hadley
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Jeroen Ooms.
In reply to this post by Duncan Murdoch-2
On Mon, Sep 23, 2013 at 6:50 PM, Duncan Murdoch
<[hidden email]> wrote:
> x <- system2("Rscript", "-e \"install.packages('MASS',
> repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)

Thank you, this suggestion seems to work (although I agree that
starting 3 procs to install a single package seems suboptimal). One
additional question: is it safe to assume that the "Rscript"
executable can always be found by the R process (cross platform), or
do I need to modify the example in case Rscript is not in the system
search path?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

hadley wickham
You shouldn't assume - use file.path(R.home("bin"), "R")

Hadley

On Tue, Sep 24, 2013 at 9:37 AM, Jeroen Ooms <[hidden email]> wrote:

> On Mon, Sep 23, 2013 at 6:50 PM, Duncan Murdoch
> <[hidden email]> wrote:
>> x <- system2("Rscript", "-e \"install.packages('MASS',
>> repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)
>
> Thank you, this suggestion seems to work (although I agree that
> starting 3 procs to install a single package seems suboptimal). One
> additional question: is it safe to assume that the "Rscript"
> executable can always be found by the R process (cross platform), or
> do I need to modify the example in case Rscript is not in the system
> search path?
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
Chief Scientist, RStudio
http://had.co.nz/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Jeroen Ooms.
In reply to this post by Simon Urbanek
On Mon, Sep 23, 2013 at 6:34 PM, Simon Urbanek
<[hidden email]> wrote:
> On top of my head for full capture I can only think of custom C code that will re-direct stderr/out FDs as desired (it's really trivial to re-direct to files, for dynamic capture it's a little more involved but there are examples that do that).

I had secretly been hoping that system2("whoami", stdout=stdout(),
stderr=stderr()) would capture output streams from the child process
and concatenate to stdout() and stderr() in the parent just as if they
were sent by cat("foo", file=stdout()) and cat("bar", file=stderr())
such that they could be captured from sink() in the parent. However
thinking twice about this, it is probably very difficult to implement
without threads. The event loop in the parent process would need to
poll for buffered output in the child while waiting for it to return
or something.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Capture output of install.packages (pipe system2)

Duncan Murdoch-2
In reply to this post by Jeroen Ooms.
On 13-09-24 10:37 AM, Jeroen Ooms wrote:
> On Mon, Sep 23, 2013 at 6:50 PM, Duncan Murdoch
> <[hidden email]> wrote:
>> x <- system2("Rscript", "-e \"install.packages('MASS',
>> repos='http://probability.ca/cran')\"", stdout=TRUE, stderr=TRUE)
>
> Thank you, this suggestion seems to work (although I agree that
> starting 3 procs to install a single package seems suboptimal).


For something that I only do every few weeks or months, I'd rather
optimize the use of the programmer's time than that of the processor.  I
get paid more per hour!

Duncan Murdoch

  One
> additional question: is it safe to assume that the "Rscript"
> executable can always be found by the R process (cross platform), or
> do I need to modify the example in case Rscript is not in the system
> search path?
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Loading...