Skip to content

Commit

Permalink
Added on.exit() call per CRAN request.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbulsink committed Nov 12, 2024
1 parent c240bc4 commit 286ca9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
29 changes: 17 additions & 12 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
#' @examples
#' # Writing a temporary file to read later
#' tf <- tempfile(fileext = ".csv")
#' write.csv(sample_spectra[sample_spectra$sample_id == "paper", c('wavenumber', 'absorbance')],
#' file = tf, row.names = FALSE)
#' write.csv(sample_spectra[sample_spectra$sample_id == "paper", c("wavenumber", "absorbance")],
#' file = tf, row.names = FALSE
#' )
#'
#' # Read the .csv file and call the sample `sample1`
#' read_ftir(tf, sample_name = "sample1")
Expand Down Expand Up @@ -119,10 +120,12 @@ read_ftir <- function(path = ".", file = NA, sample_name = NA, ...) {
#' @examples
#' # Putting some files in a temp dir to read back into PlotFTIR:
#' td <- tempdir()
#' write.csv(sample_spectra[sample_spectra$sample_id == "paper", c('wavenumber', 'absorbance')],
#' file = file.path(td, "ftir_sample_1.csv"), row.names = FALSE)
#' write.csv(sample_spectra[sample_spectra$sample_id == "toluene", c('wavenumber', 'absorbance')],
#' file = file.path(td, "ftir_sample_2.csv"), row.names = FALSE)
#' write.csv(sample_spectra[sample_spectra$sample_id == "paper", c("wavenumber", "absorbance")],
#' file = file.path(td, "ftir_sample_1.csv"), row.names = FALSE
#' )
#' write.csv(sample_spectra[sample_spectra$sample_id == "toluene", c("wavenumber", "absorbance")],
#' file = file.path(td, "ftir_sample_2.csv"), row.names = FALSE
#' )
#'
#' # Read .csv files from the temp directory and call them `sample-1` and `sample-2`
#' read_ftir_directory(td, c("ftir_sample_1.csv", "ftir_sample_2.csv"), c("sample-1", "sample-2"))
Expand Down Expand Up @@ -293,7 +296,7 @@ read_ftir_a2r <- function(path, file, sample_name = NA, ...) {
#' @export
#'
#' @examples
#' if(requireNamespace("ggplot2", quietly = TRUE)){
#' if (requireNamespace("ggplot2", quietly = TRUE)) {
#' td <- tempdir()
#' save_plot(plot_ftir(biodiesel), filename = file.path(td, "biodiesel_plot.png"))
#' }
Expand Down Expand Up @@ -393,7 +396,7 @@ ir_to_df <- function(ir, what) {
irdata <- ir::ir_get_spectrum(ir, what = what)
irdata <- mapply(cbind, irdata, "sample_id" = names(irdata), SIMPLIFY = FALSE)
irdata <- do.call(rbind, irdata)
colnames(irdata)[colnames(irdata)=='x'] <- "wavenumber"
colnames(irdata)[colnames(irdata) == "x"] <- "wavenumber"

intensity <- NA
ftir <- data.frame()
Expand Down Expand Up @@ -465,7 +468,8 @@ ir_to_df <- function(ir, what) {
#' if (requireNamespace("ir", quietly = TRUE)) {
#' # convert biodiesel to a `ir` object
#' plotftir_to_ir(biodiesel,
#' metadata = data.frame("Biodiesel_Content" = c(0, 0.25, 0.5, 1, 2.5, 5, 7.5, 10, 0.5, 5, NA)))
#' metadata = data.frame("Biodiesel_Content" = c(0, 0.25, 0.5, 1, 2.5, 5, 7.5, 10, 0.5, 5, NA))
#' )
#' }
plotftir_to_ir <- function(ftir, metadata = NA) {
# Package checks
Expand Down Expand Up @@ -555,7 +559,7 @@ plotftir_to_chemospec <- function(ftir, group_crit = NA, group_colours = "auto",
# Package checks
if (!requireNamespace("R.utils", quietly = TRUE)) {
cli::cli_abort(c("{.pkg PlotFTIR} and {.pkg ChemoSpec} requires {.pkg R.utils} package installation for this function.",
i = "Install {.pkg R.utils} with {.code install.packages('R.utils')}"
i = "Install {.pkg R.utils} with {.code install.packages('R.utils')}"
))
}

Expand Down Expand Up @@ -595,12 +599,13 @@ plotftir_to_chemospec <- function(ftir, group_crit = NA, group_colours = "auto",

intensity <- ifelse("absorbance" %in% colnames(ftir), "absorbance", "transmittance")
currentwd <- getwd()
dir<-tempdir()
on.exit(setwd(currentwd))
dir <- tempdir()
setwd(dir)

for (i in seq_along(unique(ftir$sample_id))) {
sid <- unique(ftir$sample_id)[i]
utils::write.csv(ftir[ftir$sample_id == sid, c('wavenumber', intensity)], file = paste0("./", sid, ".csv"), row.names = FALSE)
utils::write.csv(ftir[ftir$sample_id == sid, c("wavenumber", intensity)], file = paste0("./", sid, ".csv"), row.names = FALSE)
}
cs_ftir <- ChemoSpec::files2SpectraObject(gr.crit = group_crit, gr.cols = group_colours, freq.unit = "wavenumber", int.unit = intensity, fileExt = ".csv", descrip = description, header = TRUE, sep = ",", dec = ".")

Expand Down
7 changes: 6 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* with CODE_OF_CONDUCT.md references removed from readme,
* file LICENSE removed and 'cph' specified in Authors@R

* This is a resubmission with corrections made as requested by Konstanze Lauseker:
* Previously resubmitted with corrections made as requested by Konstanze Lauseker:
* Examples with \dontrun are unwrapped and (if required) wrapped by requireNamespace() checks.
* At this point no external references to these methods exist, so no change to DESCRIPTION has been made.

* This is a resubmission with changes made as requested by Beni Altmann:
* Changes to working directory were unavoidable by the interface to ChemoSpec (that package doesn't allow
for specifying directory when importing files). As such, in-code changes to working directory are protected
with a call to `on.exit()`.
11 changes: 6 additions & 5 deletions tests/testthat/test-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ test_that("interface to ir is ok", {
})

test_that("Interface to ChemoSpec is ok", {
if(!requireNamespace("R.utils", quietly = TRUE)) {
if (!requireNamespace("R.utils", quietly = TRUE)) {
expect_error(plotftir_to_chemospec(biodiesel), regexp = "requires R.utils package installation for this function.", fixed = TRUE)
testthat::skip("R.utils not available for testing interface")
}
Expand All @@ -286,8 +286,9 @@ test_that("Interface to ChemoSpec is ok", {

expect_error(plotftir_to_chemospec(biodiesel, group_colours = "blue"), regexp = ", or a vector of the same length as group_crit", fixed = TRUE)
expect_error(plotftir_to_chemospec(biodiesel, group_crit = c("biodiesel", "unknown"), group_colours = c("orange", "green", "blue")), regexp = ", or a vector of the same length as group_crit", fixed = TRUE)
expect_message(plotftir_to_chemospec(biodiesel, group_crit = c("biodiesel", "unknown"), group_colours = c('red', 'blue'), description = "This is a very long description with 57 characters in it."),
regexp = "ChemoSpec advises that description is 40 characters or less. Your description is 57 characters", fixed = TRUE)
expect_message(plotftir_to_chemospec(biodiesel, group_crit = c("biodiesel", "unknown"), group_colours = c("red", "blue"), description = "This is a very long description with 57 characters in it."),
regexp = "ChemoSpec advises that description is 40 characters or less. Your description is 57 characters", fixed = TRUE
)


expect_message(plotftir_to_chemospec(biodiesel), regexp = " to ensure enough colours available for groups.", fixed = TRUE)
Expand All @@ -296,11 +297,11 @@ test_that("Interface to ChemoSpec is ok", {
csdata <- plotftir_to_chemospec(biodiesel, group_crit = c("biodiesel", "unknown"))

expect_equal(class(csdata), "Spectra")
expect_type(csdata$data, 'double')
expect_type(csdata$data, "double")

csdata2 <- chemospec_to_plotftir(csdata)

expect_equal(csdata2[csdata2$sample_id == "biodiesel_0",], biodiesel[biodiesel$sample_id == "biodiesel_0",], ignore_attr = TRUE)
expect_equal(csdata2[csdata2$sample_id == "biodiesel_0", ], biodiesel[biodiesel$sample_id == "biodiesel_0", ], ignore_attr = TRUE)

expect_true("ggplot" %in% suppressWarnings(class(plot_ftir(SrE.IR))))
})

0 comments on commit 286ca9f

Please sign in to comment.