My Rmd script was made to convert FTIR-ATR .asc files to .txt (Chunk 1), delete the first 56 lines of the .txt (Chunk 2), create folders based on an acronym used in the file name (Chunk 3), create subfolders in the previous folders based on the numbers in the file name (Chunk 4), create a file in each of the subfolders that averages the data in those folders (Chunk 5), and finally create a graph of one of the averaged data sets to compare to a baseline dataset (which lives in a folder in the same space as those made in Chunk 3). Chunks 1-5 knit smoothly, but when I get to Chunk 6, it fails to knit the PDF and deletes the datafiles in the baseline dataset. Just running the script itself does not do this, it only seems to happen when attempting to knit to a PDF. I do have knitr::opts_chunk$set(echo = TRUE) in the beginning of the file. I get the error and script below:
Quitting from lines 215-249 [unnamed-chunk-6] (FTIR_Code.Rmd)
Error in file()
:
! cannot open the connection
Backtrace:
- utils::read.table(…)
- base::file(file, “rt”)
Execution halted
#setwd
site_folder <- 'TriMountain' # This can change to any site from Chunk 3
litter_species <- 'Maple' # This can change to any leaf litter species from the subfolders for the site made in Chunk 4
baseline_folder <- 'Baseline' # This is a baseline folder that just has datasets for the four litter species to compare to the averaged ones
baseline_data_file <- file.path(baseline_folder, paste0("CAP_", litter_species, "_Test.TXT"))
averaged_data_file <- file.path(site_folder, litter_species, paste0("Averaged_", litter_species, ".TXT"))
#This is where the error occurs
averaged_data <- read.table(averaged_data_file, sep = "t", header = FALSE, col.names = c("Wavelength", "Absorbance"))
baseline_data <- read.table(baseline_data_file, sep = "t", header = FALSE, col.names = c("Wavelength", "Absorbance"))
# Combine datasets for plotting
averaged_data$Type <- 'Decomposed'
baseline_data$Type <- 'Baseline'
combined_data <- rbind(averaged_data, baseline_data)
# Plot averaged compared to baseline
ggplot(combined_data, aes(x = Wavelength, y = Absorbance, color = Type)) +
geom_line() +
labs(title = paste("FTIR-ATR Comparison: ", litter_species),
x = "Wavelength (cm^-1)",
y = "Absorbance") +
theme_minimal() +
scale_color_manual(values = c('Decomposed' = 'blue', 'Baseline' = 'red')) +
scale_x_reverse() #FTIR data is plotted with x-axis decreasing from left to right
I am unsure why I am getting the error I get above.
I have tried to adjust the YAML header to create a desired output, but that did not work. Some of the other solutions I’ve seen tend to deal with where the file is outputted, but my file won’t knit so I can’t get to that point.