Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce plotting #78

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/extract_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ get_basic_metadata <- function(ds_list) {

get_meta_timestamp <- function(ds_list) {
text <- ds_list$history$text
history <- paste0(text, collapse = "")
history <- paste0(text, collapse = "")
save_file_time <- gsub(".*Save File\t\t(\\d.*)\t\t\t.*", "\\1", history)

time_hour_tz <- strsplit(x = save_file_time, split = " ")[[1L]]
Expand All @@ -41,7 +41,7 @@ get_meta_timestamp <- function(ds_list) {
get_meta_utc_datetime <- function(timestamp) {
tz <- timestamp$timezone
utc_diff <- as.integer(gsub("\\D+(\\+\\d)", "\\1", tz))
utc_datetime <- as.POSIXct(strptime(timestamp$datetime,
utc_datetime <- as.POSIXct(strptime(timestamp$datetime,
format = "%Y-%m-%d %H:%M:%S",
tz = "UTC"
)) - (utc_diff * 3600)
Expand Down
87 changes: 87 additions & 0 deletions plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#' Plot spectra read from OPUS files
#'
#' @param data list of class "opusreader2" (single OPUS file) or both
#' "opusreader2" and "list_opusreader2", containing spectral data parsed from
#' Bruker OPUS binary files.
#' @param data_type character vector of length one with desired spectral data
#' type to plot.
#' @param plot_type plot type, either `"base"``, printing a base plot, or
#' `"ggplot2"``, which returns a ggplot2 object.
#' @family core
#' @return a base R plot with spectra
#'
#' @export
plot <- function(data,
data_type = c(
"ref_no_atm_comp", "refl",
"ab_no_atm_comp", "ab",
"sc_sample", "ig_sample",
"sc_ref", "ig_ref"
),
plot_type = c("base", "ggplot2")) {
UseMethod("plot")
}


plot.opusreader2 <- function(data, data_type, plot_type) {
data_type <- match.arg(data_type)
plot_type <- match.arg(plot_type)

validate_plot(data, data_type, plot_type)

# get the spectra (y) and x (x-axis; e.g. wavenumber)
if (inherits(data, "list_opusreader2")) {
spectra <- do.call(
rbind,
Map(function(x) x[[y]], x = data, y = data_type)
)
} else {
spectra <- data[[data_type]]
}

ylab <- switch(
EXPR = data_type,
"ref_no_atm_comp" = "Reflectance (no atm. comp.)",
"refl" = "Reflectance",
"ab_no_atm_comp" = "Absorbance (no atm. comp.)",
"ab" = "Absorbance",
"sc_sample" = "Single channel sample",
"ig_sample" = "Interferogram sample",
"sc_ref" = "Single channel reference",
"ig_ref" = "Interferogram sample"
)

matplot(
x = x,
y = t(spectra),
type = "l",
lwd = 1.5,
xlab = xlab,
ylab = ylab
)
}

validate_plot <- function(data, data_type, plot_type) {
stopifnot(
inherits(data, "opusreader2")
)
validate_plot_data_type(data, data_type)
}

validate_plot_data_type <- function(data, data_type, plot_type) {
if (inherits(data, "list_opusreader2")) {
all_data_types <- all(
unlist(lapply(data, function(x) data_type %in% names(x)))
)
} else {
all_data_types <- data_type %in% names(data)
}

if (!isTRUE(all_data_types)) {
stop("All `data` elements need to contain `data_type`",
paste0(data_type, "."),
call. = FALSE
)
}
invisible(all_data_types)
}
2 changes: 1 addition & 1 deletion vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ For single OPUS files, there is also a lower level implementation.

```r
data_single <- read_opus_single(dsn = file)
```
```