textio: Import/export of hyperSpec objects to/from ASCII files A...

Description Usage Arguments Details Author(s) See Also Examples

Description

Besides save and load, two general ways to import and export data into hyperSpec objects exist.

Firstly, hyperSpec objects can be imported and exported as ASCII files.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
read.txt.wide(
  file = stop("file is required"),
  cols = list(spc = "I / a.u.", .wavelength = expression(lambda/nm)),
  sep = "\t",
  row.names = NULL,
  check.names = FALSE,
  ...
)

read.txt.long(
  file = stop("file is required"),
  cols = list(.wavelength = expression(lambda/nm), spc = "I / a.u."),
  header = TRUE,
  ...
)

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,
  ...
)

Arguments

file

filename or connection

cols

the column names specifying the column order.

For data import, a list with elements colname = label; for export a character vector with the colnames. Use wavelength to specify the wavelengths.

check.names

handed to read.table. Make sure this is FALSE, if the column names of the spectra are the wavelength values.

...

arguments handed to read.table and write.table, respectively.

header

the file has (shall have) a header line

object

the hyperSpec object

order

which columns should be ordered? order is used as index vector into a data.frame with columns given by cols.

na.last

handed to order by write.txt.long.

decreasing

logical vector giving the sort order

quote, sep, col.names, row.names

have their usual meaning (see read.table and write.table), but different default values.

For file import, row.names should usually be NULL so that the first column becomes a extra data column (as opposed to row names of the extra data).

col.labels

Should the column labels be used rather than the colnames?

append

Should the output be appended to an existing file?

header.lines

Toggle one or two line header (wavelengths in the second header line) for write.txt.wide

Details

Firstly, hyperSpec objects can be imported and exported as ASCII files.

A second option is using the package R.matlab which provides the functions readMat and writeMat.

hyperSpec comes with a number of pre-defined functions to import manufacturer specific file formats. For details, see vignette ("fileio").

read.spc imports Thermo Galactic's .spc file format, and ENVI files may be read using read.ENVI.

These functions are very flexible and provide lots of arguments.

If you use them to read or write manufacturer specific ASCII formats, please consider writing a wrapper function and contributing this function to hyperSpec. An example is in the “flu” vignette (see vignette ("flu", package = "hyperSpec").

Note that R accepts many packed formats for ASCII files, see connections. For .zip files, see unzip.

For further information, see the examples below, vignette ("fileio") and the documentation of R.matlab.

A second option is using the package R.matlab which provides the functions readMat and writeMat.

hyperSpec comes with a number of pre-defined functions to import manufacturer specific file formats. For details, see vignette ("file-io").

read.spc imports Thermo Galactic's .spc file format, and ENVI files may be read using read.ENVI.

These functions are very flexible and provide lots of arguments.

If you use them to read or write manufacturer specific ASCII formats, please consider writing a wrapper function and contributing this function to hyperSpec. An example is in the “flu” vignette (see vignette ("flu", package = "hyperSpec").

Note that R accepts many packed formats for ASCII files, see connections. For .zip files, see unzip.

For further information, see the examples below and the documentation of R.matlab.

Author(s)

C. Beleites

See Also

vignette ("fileio") and http://hyperspec.r-forge.r-project.org/blob/fileio.pdf, respectively

read.table and write.table

R.matlab for .mat files

read.ENVI for ENVI data

read.spc for .spc files

Manufacturer specific file formats: read.txt.Renishaw

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## Not run: vignette  ("file-io")

## export & import matlab files
if (require (R.matlab)) {
   # export to matlab file
   writeMat (paste0 (tempdir(), "/test.mat"),
             x = flu[[]], wavelength = flu@wavelength,
             label = lapply (flu@label, as.character))

   # reading 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])
}

## 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)))))

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 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)

hyperSpec documentation built on Sept. 13, 2021, 5:09 p.m.