Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created August 28, 2024 20:17
Show Gist options
  • Save quantra-go-algo/bb5d5fe1145430e37bcfbc2629d5e48f to your computer and use it in GitHub Desktop.
Save quantra-go-algo/bb5d5fe1145430e37bcfbc2629d5e48f to your computer and use it in GitHub Desktop.
# Function to open an XLSX file with error handling
open_xlsx <- function(file_path) {
# Output the Excel file in case it exists
result <- tryCatch({
# Read the Excel file
data <- read.xlsx(file_path)
# Set the date column as datetime type
data$date <- as.Date(data$date, origin = "1899-12-30")
# Return the data
return(data)
},
# Output the error in case the Excel file doesn't exist
error = function(e) {
# Print an error message
message("Error reading file:", e)
# Return a Null value
return(NULL)
})
# Return the Excel file or a Null value
return(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment