hyperSpec
objects to ASCII (text) filesR/write_txt_long.R
, R/write_txt_wide.R
write_txt.Rd
These functions write hyperSpec
objects to text files.
write_txt_long(
object,
file = "",
order = c(".rownames", ".wavelength"),
na.last = TRUE,
decreasing = FALSE,
quote = FALSE,
sep = "\t",
row.names = FALSE,
cols = NULL,
col.names = TRUE,
col.labels = FALSE,
append = FALSE,
...
)
write_txt_wide(
object,
file = "",
cols = NULL,
quote = FALSE,
sep = "\t",
row.names = FALSE,
col.names = TRUE,
header.lines = 1,
col.labels = if (header.lines == 1) FALSE else TRUE,
append = FALSE,
...
)
A hyperSpec
object to export.
Filename or connection to write the data.
Which columns should be sorted using base::order()
? The order
parameter is used as an index vector into a data.frame
with columns
specified by cols
.
Passed to base::order()
by write_txt_long
.
A logical vector specifying the sort order for columns.
These parameters have their usual meaning
as used in utils::write.table()
, but with different default values.
For file import, row.names
should usually be set to NULL
so that the first
column becomes an extra data column (instead of row names of the extra data).
Column names specifying the order of columns in the output file.
Should the column labels be used rather than the colnames?
Should the output be appended to an existing file?
Additional arguments passed to utils::write.table()
.
Toggle one or two-line headers (wavelengths in the
second header line) for write_txt_wide
.
## Export & import Matlab files
if (require(R.matlab)) {
# Export to a Matlab file
writeMat(paste0(tempdir(), "/test.mat"),
x = flu[[]], wavelength = flu@wavelength,
label = lapply(flu@label, as.character)
)
# Read a Matlab file
data <- readMat(paste0(tempdir(), "/test.mat"))
print(data)
mat <- new("hyperSpec",
spc = data$x,
wavelength = as.numeric(data$wavelength),
label = data$label[, , 1]
)
}
#> $x
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 27.15000 32.34467 33.37867 34.41933 36.53133 37.64767 38.1370
#> [2,] 66.80133 63.71533 66.71200 69.58233 72.52967 74.55833 77.0480
#> [3,] 93.14433 103.06767 106.19367 110.18633 113.24867 119.17300 121.3133
#> [4,] 130.66367 139.99833 143.79767 148.42000 152.13267 159.31033 165.0523
#> [5,] 167.26667 171.89833 177.47067 184.62467 189.75233 198.11533 205.5627
#> [6,] 198.43033 209.45800 215.78500 224.58700 232.52800 240.77133 248.0467
#> [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,] 39.17700 40.73567 41.38133 44.25133 44.12633 46.98067 49.08167
#> [2,] 80.25967 82.53867 84.49167 88.15167 91.08533 95.37233 95.53033
#> [3,] 124.67533 129.56867 134.11733 139.98667 145.02333 147.10367 150.53400
#> [4,] 168.68967 175.45900 181.58100 185.69233 190.89667 197.70833 203.64700
#> [5,] 208.41933 217.55267 224.74633 231.03567 236.35233 244.61233 251.95000
#> [6,] 256.89133 262.73900 270.27133 281.82867 287.70467 297.23500 307.59333
#> [,15] [,16] [,17] [,18] [,19] [,20] [,21]
#> [1,] 50.27433 50.10967 52.23167 53.03967 54.5190 56.22033 57.71933
#> [2,] 98.99533 101.03433 103.55800 107.02667 109.5453 111.67233 113.98733
#> [3,] 151.91733 158.18767 162.47900 167.57967 170.9957 176.37600 181.19767
#> [4,] 209.56433 214.66167 219.65333 225.73100 232.1257 236.47700 240.81533
#> [5,] 257.34633 265.54500 272.06333 280.74133 288.1057 291.88367 300.74000
#> [6,] 314.99367 324.56300 330.80033 340.89667 347.6217 355.02667 365.62333
#> [,22] [,23] [,24] [,25] [,26] [,27] [,28]
#> [1,] 59.51433 58.7450 60.09467 61.84067 62.16867 66.04933 65.2450
#> [2,] 117.03267 119.8350 123.49433 123.32367 127.00167 130.68067 132.3580
#> [3,] 181.86833 187.9893 192.12267 194.55600 198.58033 204.04367 208.4330
#> [4,] 247.19200 252.6117 258.44767 262.91767 269.74967 273.44567 279.3453
#> [5,] 307.87567 315.4297 321.89600 326.73133 334.01200 339.59933 343.8617
#> [6,] 374.35700 380.2173 391.40100 398.17300 405.42933 412.48400 421.2730
#> [,29] [,30] [,31] [,32] [,33] [,34] [,35]
#> [1,] 65.7990 66.94333 69.1360 70.92467 73.8340 74.38567 74.17333
#> [2,] 136.1647 138.39933 140.2243 141.04333 143.1313 147.17967 150.01200
#> [3,] 212.5187 215.81567 219.5087 222.67767 227.5900 229.54900 234.28167
#> [4,] 283.7437 290.86000 295.6520 300.38267 303.5927 307.91433 315.23467
#> [5,] 351.2567 357.79733 360.3600 367.96533 374.5490 381.76000 389.24100
#> [6,] 429.3627 433.35767 443.1873 447.88033 461.3393 467.12733 475.18967
#> [,36] [,37] [,38] [,39] [,40] [,41] [,42]
#> [1,] 75.53067 76.42967 76.1320 77.89067 79.36867 79.42733 82.02833
#> [2,] 153.69333 155.72800 158.4720 159.50067 160.81567 163.90967 166.25500
#> [3,] 236.97767 241.06967 245.4193 249.38867 251.92200 256.41200 261.64400
#> [4,] 319.20133 322.25767 328.2200 331.54600 337.62033 341.84300 347.36800
#> [5,] 397.79733 403.78367 408.0230 418.35533 420.61400 427.04333 433.33900
#> [6,] 481.66667 488.73567 494.0187 504.47333 515.40467 521.23867 522.64633
#> [,43] [,44] [,45] [,46] [,47] [,48] [,49]
#> [1,] 83.87833 83.81367 85.50667 86.50233 88.93733 88.99533 89.5150
#> [2,] 169.59233 170.82833 172.08767 175.21133 178.47067 179.68100 181.8287
#> [3,] 264.58433 268.46033 274.54733 275.68967 279.99767 283.71833 285.2170
#> [4,] 352.53033 358.72233 363.61833 369.33300 373.90100 377.09633 383.9850
#> [5,] 438.43633 449.57667 449.64833 455.43800 463.33200 471.47433 476.6003
#> [6,] 536.03533 544.41767 551.63333 556.71267 567.61267 572.54933 578.7267
#> [,50] [,51] [,52] [,53] [,54] [,55] [,56]
#> [1,] 90.55667 91.70633 93.5790 94.0130 94.21033 96.4420 96.62733
#> [2,] 184.42933 187.75600 187.9377 191.0570 192.50900 192.8010 195.55600
#> [3,] 290.15667 292.75667 294.9913 299.2253 301.33567 301.4620 309.15933
#> [4,] 386.89700 393.22067 398.5423 402.3163 405.33067 409.5040 412.68800
#> [5,] 480.93533 486.53833 491.9100 500.5920 502.12433 508.4073 512.70433
#> [6,] 587.74900 590.54867 600.4230 608.3580 614.03833 616.9557 621.87267
#> [,57] [,58] [,59] [,60] [,61] [,62] [,63] [,64]
#> [1,] 98.96233 98.84767 100.5683 100.4493 99.9160 99.7190 100.8863 101.4453
#> [2,] 197.89067 198.78200 200.9947 201.5223 200.2060 204.1320 204.9043 203.9883
#> [3,] 309.92833 310.05567 312.8650 314.6237 318.4220 323.1797 324.5793 325.8513
#> [4,] 418.63200 418.58267 417.9557 420.1910 428.1477 428.4030 430.1730 431.3680
#> [5,] 515.62733 522.20267 524.7247 525.2070 529.6997 531.7140 537.0060 538.2057
#> [6,] 629.80033 633.44500 637.1323 639.1617 642.8983 648.1720 651.6790 654.5397
#> [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72]
#> [1,] 102.6427 104.2297 103.1113 104.7580 103.1583 103.9427 105.3710 105.8633
#> [2,] 205.7560 204.8170 206.0910 208.6073 210.7967 210.5840 210.1903 210.5423
#> [3,] 326.6177 324.6370 326.9670 327.7893 329.9097 328.2817 328.5657 331.6910
#> [4,] 434.9550 434.4253 438.4397 439.8547 438.5417 440.2727 438.3083 440.5403
#> [5,] 543.8357 544.0853 547.9363 552.1570 550.9350 548.2033 548.1477 550.7770
#> [6,] 656.8327 659.7780 662.0563 663.1230 663.4820 670.4757 669.8870 670.0603
#> [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80]
#> [1,] 103.6797 104.0900 104.6540 105.2263 104.8043 105.8103 104.7580 103.8830
#> [2,] 209.8593 212.8273 212.5207 212.4197 211.2223 215.5637 213.4247 211.1593
#> [3,] 330.7493 335.0677 334.6253 334.2713 332.6473 333.6263 332.7240 330.8293
#> [4,] 443.2797 445.8890 444.9900 446.4053 444.7670 445.8270 446.1643 446.2410
#> [5,] 550.8607 551.7877 553.3040 552.5740 553.5453 551.1150 555.8740 554.8480
#> [6,] 671.0080 667.6123 666.3083 669.9743 675.6830 675.9787 676.6420 674.7367
#> [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88]
#> [1,] 105.3617 105.0150 105.0593 105.3493 103.9870 105.4387 104.1967 105.0980
#> [2,] 212.9753 213.9893 214.1720 213.4763 212.4887 211.7410 212.7047 212.9403
#> [3,] 332.1467 334.2557 333.9380 332.7860 331.5540 331.9737 334.1657 334.1063
#> [4,] 446.3653 442.3090 444.8933 446.9203 448.1863 446.1877 446.0927 448.2060
#> [5,] 558.4950 556.9250 559.0123 557.1283 555.6883 553.0920 554.9623 553.3000
#> [6,] 671.2343 673.6113 676.7353 677.4947 674.2803 676.6457 674.8907 673.0920
#> [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96]
#> [1,] 104.7233 106.6673 106.9497 104.7547 105.0827 105.3000 105.2130 104.7810
#> [2,] 212.1293 213.7727 213.4973 213.4640 213.1707 212.8360 211.9633 208.7987
#> [3,] 333.8570 336.5057 333.7757 334.7063 333.2027 333.3697 332.1660 330.7990
#> [4,] 446.9607 446.4583 446.6307 448.4503 448.0997 446.2220 445.8603 443.3197
#> [5,] 555.8330 555.1780 556.5237 554.7757 551.0763 553.9157 553.4520 550.7607
#> [6,] 674.8980 671.6417 672.5317 672.2803 671.0003 672.8790 674.7430 672.7247
#> [,97] [,98] [,99] [,100] [,101] [,102] [,103] [,104]
#> [1,] 104.5387 105.1327 105.1697 104.0570 106.3853 104.0797 104.4013 102.1813
#> [2,] 211.5063 209.4770 211.8597 213.2620 212.2837 211.7733 209.3907 208.8560
#> [3,] 332.4763 332.6367 330.2320 329.0633 327.7353 327.0317 326.8933 327.0880
#> [4,] 443.8383 442.6733 440.8430 444.9053 442.4323 438.7657 435.7203 436.7387
#> [5,] 551.4177 548.2610 549.3907 553.8383 549.5407 544.3870 544.4403 543.0117
#> [6,] 669.7910 672.0043 672.3397 668.7403 662.6333 666.2970 664.6327 659.9180
#> [,105] [,106] [,107] [,108] [,109] [,110] [,111] [,112]
#> [1,] 103.4417 101.7970 102.8717 102.3887 100.4190 101.1617 98.61133 98.42933
#> [2,] 208.3403 206.5063 206.7773 206.6450 205.2550 201.7470 201.98800 199.57000
#> [3,] 326.7383 324.4573 322.2523 318.6133 319.2347 318.6657 314.18900 313.58533
#> [4,] 438.1527 432.7040 433.1373 430.0993 427.1347 421.5907 421.58900 420.52567
#> [5,] 539.7383 538.8133 537.6860 532.2233 531.4020 527.5887 526.91933 519.80200
#> [6,] 660.1303 656.2373 654.1950 652.3183 647.8210 643.3813 640.79033 632.11967
#> [,113] [,114] [,115] [,116] [,117] [,118] [,119]
#> [1,] 98.5760 98.3410 98.46733 95.1490 94.7110 95.27433 94.88433
#> [2,] 200.5510 198.7677 197.03300 194.1447 193.3830 193.66467 192.51267
#> [3,] 310.7470 311.7763 306.24567 305.3137 303.6507 299.22133 301.25333
#> [4,] 417.2103 414.8137 415.02733 407.9040 404.0217 401.89667 397.83033
#> [5,] 518.6207 515.2290 511.55500 506.8747 503.2640 498.34400 494.06167
#> [6,] 633.5637 627.8190 623.21133 619.9967 613.5513 606.01267 605.92100
#> [,120] [,121] [,122] [,123] [,124] [,125] [,126]
#> [1,] 93.62133 92.46567 92.11533 91.30867 89.5390 88.2810 86.13633
#> [2,] 190.63300 186.45567 186.05433 182.30533 182.3337 180.5693 180.50633
#> [3,] 296.55200 294.46667 291.52000 288.69967 286.6547 283.8240 282.00933
#> [4,] 395.28733 391.98100 386.99833 387.28467 379.8793 377.2287 374.34933
#> [5,] 491.78467 484.60533 486.67233 479.56767 475.9010 472.1310 464.54333
#> [6,] 599.88400 593.00650 587.40150 586.38900 581.4985 574.1510 566.49250
#> [,127] [,128] [,129] [,130] [,131] [,132] [,133]
#> [1,] 86.1620 86.81733 85.95767 84.0670 86.2120 83.68333 82.56867
#> [2,] 180.2127 175.21100 171.65000 170.4167 170.3740 169.40700 164.89000
#> [3,] 279.3910 274.85267 271.53567 268.8547 265.9293 261.68533 260.63400
#> [4,] 368.4563 365.84700 360.04467 359.2973 354.6593 350.36133 346.36533
#> [5,] 457.9090 455.22167 449.28400 446.3457 442.7517 438.25067 432.26667
#> [6,] 560.2900 559.46250 550.27850 545.5435 542.7690 528.92800 528.12900
#> [,134] [,135] [,136] [,137] [,138] [,139] [,140]
#> [1,] 82.70533 81.26033 78.75233 76.67533 77.9530 77.5070 76.1640
#> [2,] 164.14133 163.17333 160.98933 159.66533 157.7463 155.1347 153.2393
#> [3,] 258.69100 254.81867 250.93133 247.61267 247.5203 244.7883 241.2600
#> [4,] 343.82067 341.66833 334.16667 330.03433 328.7680 324.6757 324.6210
#> [5,] 427.76167 422.73000 416.02133 413.34567 408.6227 403.8053 400.3250
#> [6,] 519.65350 516.54950 512.45650 502.48600 499.6525 495.0010 486.4970
#> [,141] [,142] [,143] [,144] [,145] [,146] [,147]
#> [1,] 75.6460 76.4370 74.57033 72.8790 72.80267 71.0520 70.21367
#> [2,] 151.5230 149.1967 147.49467 147.4900 145.43367 144.8190 142.43867
#> [3,] 240.2400 238.4173 233.56600 231.2630 229.50400 228.8280 225.52900
#> [4,] 318.5067 314.8400 312.79333 309.2847 306.05233 304.2447 299.30133
#> [5,] 393.7733 393.7857 386.53867 383.8693 378.65500 373.6480 372.70233
#> [6,] 481.7395 476.4720 472.37900 469.2545 466.99550 457.7630 452.32300
#> [,148] [,149] [,150] [,151] [,152] [,153] [,154]
#> [1,] 69.60533 69.98967 68.7800 68.34233 67.68467 67.2770 67.04833
#> [2,] 142.68967 142.88100 139.8377 135.47933 135.25267 135.8457 133.55900
#> [3,] 221.65767 218.32400 216.5063 213.21500 215.24567 208.5493 208.86967
#> [4,] 295.51333 292.25100 290.5580 286.98100 285.83633 283.1347 276.48133
#> [5,] 368.87433 366.15533 362.8997 357.86333 354.96700 352.3707 344.31567
#> [6,] 450.48350 445.14500 444.2740 435.22800 430.33300 424.7200 420.74800
#> [,155] [,156] [,157] [,158] [,159] [,160] [,161] [,162]
#> [1,] 65.3130 64.5090 63.50567 62.18367 62.0450 62.02733 61.7990 60.5280
#> [2,] 133.3177 132.8240 128.93033 125.20300 124.7397 124.71667 121.6783 120.9017
#> [3,] 210.3090 205.6747 203.98400 200.07500 197.6877 197.21600 193.0293 191.2113
#> [4,] 275.8033 272.5720 270.89733 266.44633 265.4230 262.04033 259.8753 256.7253
#> [5,] 342.1013 339.0143 336.71533 334.59300 326.0983 324.69400 320.5670 315.9307
#> [6,] 418.4665 414.0175 408.32400 403.85400 405.0045 394.33500 389.9775 384.4910
#> [,163] [,164] [,165] [,166] [,167] [,168] [,169]
#> [1,] 59.3420 59.1250 57.7480 57.44667 57.74267 56.2750 55.49233
#> [2,] 121.4060 117.6677 117.0410 115.94100 112.22900 112.7243 111.67267
#> [3,] 190.7627 186.5793 183.0927 182.39100 179.87400 177.1340 176.43600
#> [4,] 250.2083 249.2670 245.6137 240.69333 240.84900 233.9953 233.73500
#> [5,] 312.7320 307.9437 306.3863 298.34667 298.57433 296.0523 292.49933
#> [6,] 382.3370 375.0820 369.0990 366.06300 362.45350 358.9965 352.14300
#> [,170] [,171] [,172] [,173] [,174] [,175] [,176]
#> [1,] 54.40933 53.83267 53.26333 52.45733 52.1400 49.78433 49.6230
#> [2,] 109.69833 107.64067 108.05067 105.22000 102.9530 102.75833 100.6280
#> [3,] 173.72833 170.26000 168.65033 166.09067 164.4463 160.81267 158.9110
#> [4,] 230.80467 228.26600 223.37333 221.35733 218.9017 214.44667 210.0177
#> [5,] 285.46933 282.62833 279.92767 273.34667 267.5543 266.10100 261.7657
#> [6,] 348.20000 347.10650 338.79150 335.34700 330.0400 325.16200 322.0685
#> [,177] [,178] [,179] [,180] [,181]
#> [1,] 48.3380 47.30400 47.16267 46.41233 45.25633
#> [2,] 97.9650 97.35333 96.60167 96.20600 94.61033
#> [3,] 156.7613 154.92067 149.53900 148.52667 145.79333
#> [4,] 206.0153 204.57500 201.48433 198.86733 195.86733
#> [5,] 259.4020 256.20067 252.06567 248.06700 246.95200
#> [6,] 315.6455 312.32950 307.51850 302.32550 294.64950
#>
#> $wavelength
#> [1] 405.0 405.5 406.0 406.5 407.0 407.5 408.0 408.5 409.0 409.5 410.0 410.5
#> [13] 411.0 411.5 412.0 412.5 413.0 413.5 414.0 414.5 415.0 415.5 416.0 416.5
#> [25] 417.0 417.5 418.0 418.5 419.0 419.5 420.0 420.5 421.0 421.5 422.0 422.5
#> [37] 423.0 423.5 424.0 424.5 425.0 425.5 426.0 426.5 427.0 427.5 428.0 428.5
#> [49] 429.0 429.5 430.0 430.5 431.0 431.5 432.0 432.5 433.0 433.5 434.0 434.5
#> [61] 435.0 435.5 436.0 436.5 437.0 437.5 438.0 438.5 439.0 439.5 440.0 440.5
#> [73] 441.0 441.5 442.0 442.5 443.0 443.5 444.0 444.5 445.0 445.5 446.0 446.5
#> [85] 447.0 447.5 448.0 448.5 449.0 449.5 450.0 450.5 451.0 451.5 452.0 452.5
#> [97] 453.0 453.5 454.0 454.5 455.0 455.5 456.0 456.5 457.0 457.5 458.0 458.5
#> [109] 459.0 459.5 460.0 460.5 461.0 461.5 462.0 462.5 463.0 463.5 464.0 464.5
#> [121] 465.0 465.5 466.0 466.5 467.0 467.5 468.0 468.5 469.0 469.5 470.0 470.5
#> [133] 471.0 471.5 472.0 472.5 473.0 473.5 474.0 474.5 475.0 475.5 476.0 476.5
#> [145] 477.0 477.5 478.0 478.5 479.0 479.5 480.0 480.5 481.0 481.5 482.0 482.5
#> [157] 483.0 483.5 484.0 484.5 485.0 485.5 486.0 486.5 487.0 487.5 488.0 488.5
#> [169] 489.0 489.5 490.0 490.5 491.0 491.5 492.0 492.5 493.0 493.5 494.0 494.5
#> [181] 495.0
#>
#> $label
#> , , 1
#>
#> [,1]
#> .wavelength "lambda/nm"
#> spc "I[fl]/"a.u.""
#> filename "filename"
#> c "c / (mg / l)"
#>
#>
#> attr(,"header")
#> attr(,"header")$description
#> [1] "MATLAB 5.0 MAT-file, Platform: unix, Software: R v4.4.0, Created on: Mon May 27 17:09:41 2024 "
#>
#> attr(,"header")$version
#> [1] "5"
#>
#> attr(,"header")$endian
#> [1] "little"
#>
## ASCII export & import
write_txt_long(flu,
file = paste0(tempdir(), "/flu.txt"),
cols = c(".wavelength", "spc", "c"),
order = c("c", ".wavelength"),
decreasing = c(FALSE, TRUE)
)
read.txt.long(
file = paste0(tempdir(), "/flu.txt"),
cols = list(
.wavelength = expression(lambda / nm),
spc = "I / a.u", c = expression("/"(c, (mg / l)))
)
)
#> Warning: Function 'read.txt.long' is deprecated.
#> Use function 'read_txt_long' instead.
#> hyperSpec object
#> 6 spectra
#> 3 data columns
#> 181 data points / spectrum
write_txt_wide(flu,
file = paste0(tempdir(), "/flu.txt"),
cols = c("c", "spc"),
col.labels = TRUE, header.lines = 2, row.names = TRUE
)
write_txt_wide(flu,
file = paste0(tempdir(), "/flu.txt"),
col.labels = FALSE, row.names = FALSE
)
read.txt.wide(
file = paste0(tempdir(), "/flu.txt"),
# Give columns in the same order as they are in the file
cols = list(
spc = "I / a.u",
c = expression("/"("c", "mg/l")),
filename = "filename",
# Plus wavelength label last
.wavelength = "lambda / nm"
),
header = TRUE
)
#> Warning: Function 'read.txt.wide' is deprecated.
#> Use function 'read_txt_wide' instead.
#> Warning: $filename already exists. => Skipping file.keep.name
#> hyperSpec object
#> 6 spectra
#> 3 data columns
#> 181 data points / spectrum