Skip to contents

Functions for reading and writing spectral data to and from OpenSpecy format. OpenSpecy objects are lists with components wavenumber, spectra, and metadata. Currently supported formats are .y(a)ml, .json, .csv, or .rds.

Usage

write_spec(x, ...)

# S3 method for default
write_spec(x, ...)

# S3 method for OpenSpecy
write_spec(x, file, method = NULL, digits = getOption("digits"), ...)

read_spec(file, share = NULL, method = NULL, ...)

as_hyperSpec(x)

Arguments

x

an object of class OpenSpecy.

file

file path to be read from or written to.

method

optional; function to be used as a custom reader or writer. Defaults to the appropriate function based on the file extension.

digits

number of significant digits to use when formatting numeric values; defaults to getOption("digits").

share

defaults to NULL; needed to share spectra with the Open Specy community; see share_spec() for details.

...

further arguments passed to the submethods.

Value

read_spec() reads data formatted as an OpenSpecy object and returns a list object of class OpenSpecy containing spectral data. write_spec() writes a file for an object of class OpenSpecy containing spectral data. as_hyperspec() converts an OpenSpecy object to a hyperSpec-class object.

Details

Due to floating point number errors there may be some differences in the precision of the numbers returned if using multiple devices for .json and .yaml files but the numbers should be nearly identical. readRDS() should return the exact same object every time.

See also

OpenSpecy(); read_text(), read_asp(), read_spa(), read_spc(), and read_jdx() for text files, .asp, .spa, .spa, .spc, and .jdx formats, respectively; read_zip() and read_any() for wrapper functions; saveRDS(); readRDS(); write_yaml(); read_yaml(); write_json(); read_json();

Author

Zacharias Steinmetz, Win Cowger

Examples

read_extdata("raman_hdpe.yml") |> read_spec()
#>      wavenumber intensity
#>           <num>     <int>
#>   1:    301.040        26
#>   2:    304.632        50
#>   3:    308.221        48
#>   4:    311.810        45
#>   5:    315.398        46
#>  ---                     
#> 960:   3187.990        71
#> 961:   3190.520        71
#> 962:   3193.060        75
#> 963:   3195.590        75
#> 964:   3198.120        67
#> 
#> $metadata
#>        x     y  user_name spectrum_type spectrum_identity      organization
#>    <int> <int>     <char>        <char>            <char>            <char>
#> 1:     1     1 Win Cowger         Raman              HDPE Horiba Scientific
#>     license                                                        session_id
#>      <char>                                                            <char>
#> 1: CC BY-NC 5728ddde4f649fd71f6f487fc5ad8d80/dc85257201307a131e71d9ec24aaccbf
#>                             file_id      file_name    col_id
#>                              <char>         <char>    <char>
#> 1: cb06ce2846b119d932fb6696479a445b raman_hdpe.yml intensity
read_extdata("raman_hdpe.json") |> read_spec()
#>      wavenumber intensity
#>           <num>     <int>
#>   1:    301.040        26
#>   2:    304.632        50
#>   3:    308.221        48
#>   4:    311.810        45
#>   5:    315.398        46
#>  ---                     
#> 960:   3187.990        71
#> 961:   3190.520        71
#> 962:   3193.060        75
#> 963:   3195.590        75
#> 964:   3198.120        67
#> 
#> $metadata
#>        x     y  user_name spectrum_type spectrum_identity      organization
#>    <int> <int>     <char>        <char>            <char>            <char>
#> 1:     1     1 Win Cowger         Raman              HDPE Horiba Scientific
#>     license                                                        session_id
#>      <char>                                                            <char>
#> 1: CC BY-NC 5728ddde4f649fd71f6f487fc5ad8d80/dc85257201307a131e71d9ec24aaccbf
#>                             file_id       file_name    col_id
#>                              <char>          <char>    <char>
#> 1: cb06ce2846b119d932fb6696479a445b raman_hdpe.json intensity
read_extdata("raman_hdpe.rds") |> read_spec()
#>      wavenumber intensity
#>           <num>     <int>
#>   1:    301.040        26
#>   2:    304.632        50
#>   3:    308.221        48
#>   4:    311.810        45
#>   5:    315.398        46
#>  ---                     
#> 960:   3187.990        71
#> 961:   3190.520        71
#> 962:   3193.060        75
#> 963:   3195.590        75
#> 964:   3198.120        67
#> 
#> $metadata
#>        x     y  user_name spectrum_type spectrum_identity      organization
#>    <int> <int>     <char>        <char>            <char>            <char>
#> 1:     1     1 Win Cowger         Raman              HDPE Horiba Scientific
#>     license                                                        session_id
#>      <char>                                                            <char>
#> 1: CC BY-NC 5728ddde4f649fd71f6f487fc5ad8d80/dc85257201307a131e71d9ec24aaccbf
#>                             file_id      file_name
#>                              <char>         <char>
#> 1: cb06ce2846b119d932fb6696479a445b raman_hdpe.rds
read_extdata("raman_hdpe.csv") |> read_spec()
#>      wavenumber intensity
#>           <num>     <int>
#>   1:    301.040        26
#>   2:    304.632        50
#>   3:    308.221        48
#>   4:    311.810        45
#>   5:    315.398        46
#>  ---                     
#> 960:   3187.990        71
#> 961:   3190.520        71
#> 962:   3193.060        75
#> 963:   3195.590        75
#> 964:   3198.120        67
#> 
#> $metadata
#>        x     y      file_name  license    col_id
#>    <int> <int>         <char>   <char>    <char>
#> 1:     1     1 raman_hdpe.csv CC BY-NC intensity
#>                                                           session_id
#>                                                               <char>
#> 1: 919e3e54e4c2f1b1e4db64651b451179/7b524d8ec8dc80c61977c331bd1e6f3d
#>                             file_id
#>                              <char>
#> 1: cb06ce2846b119d932fb6696479a445b

if (FALSE) {
data(raman_hdpe)
write_spec(raman_hdpe, "raman_hdpe.yml")
write_spec(raman_hdpe, "raman_hdpe.json")
write_spec(raman_hdpe, "raman_hdpe.rds")
write_spec(raman_hdpe, "raman_hdpe.csv")

# Convert an OpenSpecy object to a hyperSpec object
hyper <- as_hyperSpec(raman_hdpe)
}