Created
August 28, 2024 20:17
-
-
Save quantra-go-algo/bb5d5fe1145430e37bcfbc2629d5e48f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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