[R] sink behavior

jim holtman jholtman at gmail.com
Tue Aug 7 02:54:36 CEST 2007


'sink' will capture 'printed' output from your program.  Try:

> # Using a matrix because as a simple example.
> dumpMatrix = function(mat) {
>    sink(file = "mat.txt")
>    print(mat)
>    sink(NULL)
> }

In this case, there is an explicit 'print' statement.  At the command
line, there is an implicit 'print' when you give an object name.


On 8/6/07, Daniel Gatti <dmgatti at email.unc.edu> wrote:
> There is a package called 'safe' that produces an object which I can
> only write to a file using the sink() function.  It works fine if the
> sink() command is not inside of a function, but it does not write
> anything to the file if the command is within a function.
>
> Sample code:
> # Using a matrix because as a simple example.
> dumpMatrix = function(mat) {
>    sink(file = "mat.txt")
>    mat
>    sink(NULL)
> }
>
> # This will write the file correctly.
> x = matrix(100, 10, 10)
> sink(file = "x.txt")
> x
> sink(NULL)
>
> # This will create an empty file.
> dumpMatrix(x)
>
> R 2.5.1
> Windows XP, SP2
>
> The sink() docs are full of warnings, but I'm not clear which one I've
> violated with this example.
>
> Dan
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list