Search code examples
rmatrixexport-to-csvconfusion-matrix

Capture.output disregarding console size in r


I'm trying to use capture.output to save a confusion matrix as follows:

capture.output(result, file = "F:/Results/result.txt")
  • My confusion matrix has a lot of columns
  • When I save the file, it breaks the columns as it does in the RStudio console.
  • If I make the console bigger, it has enough space and does not break
  • My question is:

Is it possible to have Capture.output ignore the console size and assume it's as big as possible (so it doesn't break things apart)?

If you want to try it on my specific data, I put it here as an .rds file: https://upload.uni-jena.de/data/5f1045c6c0da81.96434142/result.rds

Here's how it looks:

In a small console (broken apart):

Confusion Matrix and Statistics

           Reference
Prediction  ATCC25922 DSM1576
  ATCC25922         2       0
  DSM1576           0       2
  DSM3871           0       0
  DSM429            0       0
  DSM498            0       0
  DSM499            0       0
  DSM501            0       0
  DSM613            0       0
  DSM8696           0       0
  DSM9031           0       0
  Gr02              0       0
  Gr05              0       0
  Gr06              0       0
  Gr07              0       0
  Gr09              0       1
  Pak56             0       0
  Pak57             0       0
  Pak58             1       0
  Pak64             0       0
  Pak66             0       0
           Reference
Prediction  DSM3871 DSM429
  ATCC25922       0      0
  DSM1576         0      1
  DSM3871         2      0
  DSM429          0      2
  DSM498          1      0
  DSM499          0      0
  DSM501          0      0
  DSM613          0      0
  DSM8696         0      0
  DSM9031         0      0
  Gr02            0      0
  Gr05            0      0
  Gr06            0      0
  Gr07            0      0
  Gr09            0      0
  Pak56           0      0
  Pak57           0      0
  Pak58           0      0
  Pak64           0      0
  Pak66           0      0
           Reference
Prediction  DSM498 DSM499
  ATCC25922      0      0
  DSM1576        0      0
  DSM3871        0      0
  DSM429         0      0
  DSM498         0      0
  DSM499         0      3
  DSM501         0      0
  DSM613         0      0
  DSM8696        0      0
  DSM9031        0      0
  Gr02           0      0
  Gr05           0      0
  Gr06           0      0
  Gr07           1      0
  Gr09           1      0
  Pak56          0      0
  Pak57          0      0
  Pak58          1      0
  Pak64          0      0
  Pak66          0      0
...

In a large console (in one piece):

Confusion Matrix and Statistics

           Reference
Prediction  ATCC25922 DSM1576 DSM3871 DSM429 DSM498 DSM499 DSM501 DSM613 DSM8696 DSM9031 Gr02 Gr05 Gr06 Gr07 Gr09 Pak56 Pak57 Pak58 Pak64 Pak66
  ATCC25922         2       0       0      0      0      0      0      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM1576           0       2       0      1      0      0      0      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM3871           0       0       2      0      0      0      0      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM429            0       0       0      2      0      0      1      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM498            0       0       1      0      0      0      0      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM499            0       0       0      0      0      3      0      0       0       1    0    0    0    0    1     0     0     0     0     0
  DSM501            0       0       0      0      0      0      2      0       0       0    0    0    0    0    0     0     0     0     0     0
  DSM613            0       0       0      0      0      0      0      3       0       0    0    0    0    0    0     0     0     0     0     0
  DSM8696           0       0       0      0      0      0      0      0       3       0    0    0    0    0    1     0     0     0     0     0
  DSM9031           0       0       0      0      0      0      0      0       0       2    0    0    0    0    0     0     0     0     0     1
  Gr02              0       0       0      0      0      0      0      0       0       0    3    0    0    0    0     0     0     0     0     0
  Gr05              0       0       0      0      0      0      0      0       0       0    0    2    0    0    0     0     0     0     0     0
  Gr06              0       0       0      0      0      0      0      0       0       0    0    1    2    0    0     0     0     0     0     0
  Gr07              0       0       0      0      1      0      0      0       0       0    0    0    0    1    0     0     1     0     0     0
  Gr09              0       1       0      0      1      0      0      0       0       0    0    0    1    0    1     0     1     0     1     0
  Pak56             0       0       0      0      0      0      0      0       0       0    0    0    0    0    0     3     0     0     0     0
  Pak57             0       0       0      0      0      0      0      0       0       0    0    0    0    0    0     0     1     1     0     0
  Pak58             1       0       0      0      1      0      0      0       0       0    0    0    0    1    0     0     0     1     1     0
  Pak64             0       0       0      0      0      0      0      0       0       0    0    0    0    0    0     0     0     0     1     0
  Pak66             0       0       0      0      0      0      0      0       0       0    0    0    0    1    0     0     0     1     0     2

Solution

  • Set options(width = 100) to enlarge the maximum number of characters on a line used in printing objects on the console.

    options(width = 100)
    capture.output(result, file = "F:/Results/result.txt")
    

    Note: Don't resize the console window after setting options(width = 100). Some R consoles automatically change the value when they are resized.