0

I have a source code and want to output the result to a txt file. I manage to do this using the sink() function, like bellow:

    source("Script sink test.txt", echo=F)
    loopsinktestA <- file("SinktestA.txt")
    sink(loopsinktestA, append=TRUE)
    sink(loopsinktestA, append=TRUE, type="output")
    Loop(MMatrix, "Parameter1", "Parameter2")
    sink()

And all works well except one detail. In my function output, there are some matrices, which tend to be big. When I put the results to appear on the console, the matrices tend to break according to with space on the screen, which is quite normal (if I have a bigger space, by pushing the help/graphs to the far right, more columns will appear before the matrix is broken). My problem is, the exact same thing happens in the SinktestA.txt, basically, it mirrors what it would be expected in the console (I tried different exports and giving more and less space in the console, and the results in the output file broke the matrices in smaller or bigger number of columns).

E.g., this is how it's shown in the output due to the matrix not fitting all of the columns side by side:

          Fi         M      Pi
    As   16.06343  32.42949 0.00000
    Ce   59.48432  39.78937 0.01211
    Ri    0.01211   0.01211 0.00000
    Ast 100.00000 100.00000 0.00000
    Ts   25.11802  45.56349 0.00000
    At    0.00000   0.00000 0.00000
    Dm    7.17831  28.16850 0.00000

          M        Pi
    As   0.00000  99.98789
    Ce   0.00000  99.98789
    Ri  99.98789 100.00000
    Ast  0.00000 100.00000
    Ts   0.00000  99.98789
    At   0.00000  99.98789
    Dm   0.00000  99.98789

Note: I actually have more columns, but for example purpose, le consider my space on the console just shows 4 columns and after has to break the matrix.

My question is, is there a way, or argument that I can use indicating the number of characters or something to be used before the matrices are broken in the output? Or a way to indicate the output should mirror the output results but not exactly its format?

Sorry if not that clear at any point.

  • are you sure you want to use `sink()` for your use case? As you pointed out it mirrors what shows in the console. You could have more flexibility if your use case could be handled with `write()`, `write.csv()`, other base write functions. – Adam Spannbauer May 12 '17 at 14:17
  • 1
    @AdamSpannbauer the thing is, my Loop function is supposed to print not just one matrix, but multiple, plus some other results, like simple mathematical operations like sums, and reports. When I tried the `write()` or `write.csv()` I didn´t get the results I wanted. But I found another way around jus now, using the `options(width=...)` function and it works. Found in a comment here [link](https://stackoverflow.com/questions/20272137/r-sink-split-table-in-some-lines?rq=1). – Daniel Pardejo May 12 '17 at 14:28

0 Answers0