GithubHelp home page GithubHelp logo

Get both stdout and stderr about callr HOT 7 CLOSED

r-lib avatar r-lib commented on June 12, 2024
Get both stdout and stderr

from callr.

Comments (7)

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Sorry for the slow response.

Writing both stdout and stderr to the same file is tricky, because of non-atomic IO, possible race conditions, the buffering of stdout, possible more problems.

Making this to work across platforms is not an easy task imo.

But if you have some ideas, I would be happy to hear them.

from callr.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Maybe if we just redirect with 2&>1 >file that works well enough? Let me give it a try....

from callr.

mllg avatar mllg commented on June 12, 2024

Sorry for not responding in time.
Another possibility would be to just wrap the expression in a R function which calls sink twice (for stdout and stderr):

wrapper = function(expr, dst) {
  fp = file(dst, open = "wt")
  sink(file = fp)
  sink(file = fp, type = "message")
  on.exit({ sink(type = "message"); sink(type = "output"); close(fp) })
  expr
}

This is basically what I use in batchtools and it works quite okay most of the time. Your solution might be even better, because you could also record output from C which does not use Rprintf etc. However, the wrapper might be a nice fallback for windows where you do not have pipes and redirections (or do you?).

from callr.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

sink would work for r, but not for rcmd, because that just runs R CMD. So I would prefer an approach that works for both, because they both use the same safe_system internally.

Redirection works fine on windows, even 2>&1 is fine, in fact R itself uses redirection in system:

...
    if (ignore.stdout)
        command <- paste(command, ">/dev/null")
    if (ignore.stderr)
        command <- paste(command, "2>/dev/null")
...

So I think this will be fine. r (and rcmd) both return stdout and stderr separately, and I would keep this. But if you specify the same file for stdout and stderr, then I would do 2>&1.

from callr.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Oh, wait. If I redirect both to the same file, then I cannot return them separately.....

from callr.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

OK, looks like there is no way to collect stdout and stderr in the right order...... sink() would be the only way, i.e. within R, but that does not work for rcmd().

I can "fix" the current behavior, in that I can make sure that if you redirect output and error to the same file, then they will be in the file, although in the wrong order....

from callr.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Hmmm, looking at this again, r does not return the output, actually....
So I guess we could use sink() for it.

from callr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.