Skip to content

Instantly share code, notes, and snippets.

@philipp-baumann
Last active December 29, 2017 11:47
Show Gist options
  • Save philipp-baumann/c7e347a286eb57b1498b8c8a195b3658 to your computer and use it in GitHub Desktop.
Save philipp-baumann/c7e347a286eb57b1498b8c8a195b3658 to your computer and use it in GitHub Desktop.
Visualize spectra
################################################################################
## Task: Example script to visualize spectra in simplerspec framework
################################################################################
# Remove all R objects from memory
rm(list = ls())
# Load packages
pkgs <- c("tidyverse", "simplerspec")
lapply(pkgs, library, character.only = TRUE)
## Read spectra from OPUS files ================================================
# Create vector with list of OPUS files to be read
lf <- dir(path = "data/spectra", full.names = TRUE)
# Read into list
spc_l <- read_opus_univ(fnames = lf, parallel = FALSE)
# Possible to read in parallel to increase speed; see
## Gather and process spectra ==================================================
# Spectral processing pipe
spc_tbl <- spc_l %>%
# Gather list of spectra data into tibble data frame
gather_spc()
# Investigate spectral tibble (data.frame)
# Get raw spectrum from first sample
dim(spc_tbl$spc[[1]])
spc_tbl <- spc_tbl %>%
# Resample spectra to new wavenumber interval
resample_spc(wn_lower = 500, wn_upper = 3996, wn_interval = 2) %>%
# Average replicate scans per sample_id
average_spc() %>%
# Preprocess spectra using Savitzky-Golay first derivative with a window size
# of 21 points
preprocess_spc(select = "sg_1_w21")
# Print preprocessed spectral tibble (data.frame)
spc_tbl
## Exploratory data analysis ===================================================
spc_tbl %>%
plot_spc_ext(group_panel = FALSE, group_color = FALSE)
# Check which sample has absorbance above 2
spc_m <- do.call(rbind, spc_tbl$spc)
# -> manually remove; check in OPUS software
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment