Added eval; site now shows clean dataset missing message instead of server error...
[sgn.git] / R / Nirs / nirs_visualize_spectra.R
blobf44f87a436c8ec137d3fb92b4ce44d22ab2a4e32
1 # Visualize spectra
2 # Jenna Hershberger
3 # jmh579@cornell.edu
4 # 10/02/2020
6 library(tidyverse)
7 library(waves)
8 library(jsonlite)
10 #### Read in raw JSON ####
11 # args:
12 # 1. Input JSON filepath
13 # 2. Output filepath for .png plot of spectra
15 args <- commandArgs(trailingOnly = TRUE)
17 raw.spectra <- jsonlite::fromJSON(txt = args[1], flatten = T) %>%
18   type_convert(cols(.default = col_double(), observationUnitId = col_character(),
19   device_type = col_character())) %>%
20   dplyr::select(observationUnitId, device_type, starts_with("nirs_spectra"))
22 wls <- colnames(raw.spectra) %>%
23   str_subset("nirs_spectra.") %>%
24   str_remove("nirs_spectra.") %>%
25   readr::parse_number() %>%
26   sort()
28 raw.spectra <- raw.spectra %>%
29     dplyr::select(observationUnitId, device_type, paste0("nirs_spectra.X", wls))
31 #### Generate plot and identify outliers ####
32 spec.plot <- raw.spectra %>%
33   rename_at(vars(starts_with("nirs_spectra")), ~str_replace(., "nirs_spectra.", "")) %>%
34   rownames_to_column(var = "unique.id") %>%
35   dplyr::select(-device_type) %>%
36   plot_spectra(wls, num.col.before.spectra = 3, window.size = 100)
38 #### Output plot ####
39 ggsave(plot = spec.plot, filename = args[2], units = "in", height = 7, width = 10)