Im running into an issue with my R script, where read.csv fails to read a csv from a file path. Essentially the goal of my script is to build a file path based on the step variable input. My issue is that it will create the correct file path. But R fails to open the connection. This is the part of the script where it builds the file path:
library(tidyverse)
library(ggplot2)
library(readxl)
# ==================== #
Step <- "pntSetting"
# Step Check
properParam <- c('Cleaning', 100, 150, 175, 200, 225, 'pntSetting', 'Final')
Param <- c('Cleaning', 'pntSetting', 'Final')
if(!tolower(Step) %in% tolower(properParam)) { # If Step does not match anything in properParams regardless of capitalization
stop(paste0('Step needs to be ', paste(properParam, collapse = ", "))) # Stop script and print the appropriate options
} else if (!Step %in% properParam) { # If Step does not match anything in properParams regarding capitalization
Step <- Param[grep(tolower(Step), tolower(Param))] # use grep() to replace Step with the appropriate Step with correct capitalization
} else if (is.numeric(Step)) { # If Step is of numeric data type
Step <- as.character(Step) # convert to character data type
}
# ==================== #
colKeep <- c("LongLat_ID", "LAT_GPS", "LONG_GPS", "perflac_all", "strSize", "lc01", "lc02", "lc03", "lc04", "lc05", "lc06", "lc07", "lc08", "lc09")
colRm <- c("LongLat_ID", "LAT_GPS", "LONG_GPS")
# Folder Paths
# Data Dictionaries
paramVers <- list( 'Cleaning' = 0,
'100' = 1,
'150' = 2,
'175' = 3,
'200' = 4,
'225' = 5,
'pntSetting' = 5.5,
'Final' = 6 )
OutSuffix <- list('0' = "_Cleaning",
'1' = "_100m",
'2' = "_150m",
'3' = "_175m",
'4' = "_200m",
'5' = "_225m",
'5.5' = "_pntSeting",
'6' = "_Final" )
InSuffix <- list( '0' = "_Cleaning",
'1' = "_100m",
'2' = "_150m",
'3' = "_175m",
'4' = "_200m",
'5' = "_225m",
'5.5' = "_255m",
'6' = "_Final" )
Version <- paramVers[[Step]]
inputsuffix <- InSuffix[[as.character(Version)]]
outputsuffix <- OutSuffix[[as.character(Version)]]
if(Step == 'pntSetting'){
iteration <- paste0("A", (Version - 0.5)) # Using "Version to create the file prefix
} else {
iteration <- paste0("A", Version) # Using "Version to create the file prefix
}
# Base file paths
base_path <- file.path("Data", "Analysis") # Data/Analysis
folder <- paste0(iteration, inputsuffix)
file_path <- file.path(base_path, folder) # Data/Analysis/A?_[inputSuffix]
file_path
dir.exists(file_path)
# file_path <- file.path(base_path, paste0(iteration, inputsuffix))
repFile_path <- file.path(file_path, "Adjust") # Data/Analysis/A?_[inputSuffix]/Adjust
snapVal_path <- file.path(base_path, "FullDat") # Data/Analysis/FullDat
# File Paths
# File prior to pntSetting
input_file <- file.path(file_path, paste0(iteration, "_QISBLongLatNA.csv")) # csv with the most up to unsnapped station prior to adjustment
# Adjustment Files
ReplaceMissingCoords <- file.path(repFile_path, "ReplaceMissingCoords.csv") # csv for the unsnapped stations
ReAdjustWrongCoords <- file.path(repFile_path, "ReAdjustCoords.csv") # csv with readjusted coordinates of already snapped stations
HydroMisMatch <- file.path(repFile_path, "HydroMismatch.csv") # csv with hydrological mismatched stations.
# QISB Files
snapVal <- file.path(snapVal_path, paste0(iteration, "_QISB.csv")) # The most recent QISB file
repFile_path
dir.exists(repFile_path)
ReplaceMissCoord <- read.csv(ReplaceMissingCoords) # Manually Snapped Missing Sample Sites
ReAdjustCoords <- read.csv(ReAdjustWrongCoords) # Sample Sites with Manually adjusted Coordinates.
pntsRemove <- read.csv(HydroMisMatch) # Confirmed Hydrological Mismatched
When I run this part of my script. receive this error:
Error in file(file, “rt”) : cannot open the connection
What I’ve been testing is using “pntSetting” as my “step” parameter, which would give me values of 5.5, _255m, and _pntSetting, for “Version”, “inputsuffix” and “outputsuffix” variables, respectively.
The overall file path looks like this: “Data/Analysis/A5_225m/Adjust”
By recreating different versions of the folder path, I’ve deduced the problem down to my “inputsuffix” variable (“_255m” in file path example). I have successfully been able to have the file read when I manually type in “_255m”, but if I use the inputsuffix variable, it will throw the error. The Version variable does not seem to be a problem, as it works in all cases. I have also checked for any typos, and the folder path is correct. I’ve also tried absolute paths (including and excluding working directory path) to test, and the file reads.
Is anyone familiar with this issue, could it be because of my data dictionaries that I’m using to generate folder paths?